Solved

How to run an applications installer class from outside the application, in C#?

Posted on 2010-11-18
11
356 Views
Last Modified: 2012-05-10
Hi,

I have a few applications written in C#, one of them being a Windows Service.

I am now in the process of writing an automated installation project, and I've hit a stumbling block in that I need a simple and robust way of installing the Windows Service through C# code.

The Windows Service, in the past, has used its own Setup project to install it.

From what I can gather, the actual installation of the Service (i.e. registering it in Windows to show in services.msc) is all done by the Setup project calling the Install method of the Installer class in the Service project.

How can I do this from my own C# code?

I would also want to call the Uninstall on the Install class too.
0
Comment
Question by:JimR123b
  • 6
  • 5
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34163032
The simplest way to do it would be to shell out to "installutil", the utility for installing services. You simply call installutil with the "-i" switch ("-u" to uninstall) and pass it path/name of the EXE for your service.
0
 

Author Comment

by:JimR123b
ID: 34163053
Hi. I had considered using installutil, but there is other code in the Service project's Installer class that I also want running.

Also, I need the process to be invisible, as it will be running as a service itself, and should install/reinstall other services behind the scenes. Shelling out to anything is less than desirable as it would result in something happening on the user's screen.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34163075
0
 

Author Comment

by:JimR123b
ID: 34163117
Does that actually run the code in the Installer class though?

I perhaps didn't make it clear that I'd like to be able to use the Installer class code as that means I can use a common approach to install all applications, be them Windows Service or WinForms applications.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34163226
I don't think so because it is registering it directly with Windows.

This might be more along the lines of what you're looking for:

    http://dotnetstep.blogspot.com/2009/06/programmatically-install-window-service.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:JimR123b
ID: 34163461
That looks like a very useful code example, but it doesn't seem to call the Service project's installer class, which I'd prefer to do if I can. Could be something to fall back on though, assuming it works in .Net 3.5...

Is there any way to call the Install Method on another assembly's Installer class?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34163493
As long as it is public you should be able to. Although I would imagine you would have to use reflection in order to load the assembly, locate the correct class and then invoke it.
0
 

Author Comment

by:JimR123b
ID: 34163548
...and how do I do that please?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34163749
Something along the following lines worked for me:
        static void Main(string[] args)
        {
            string asmPath = @"C:\projects\dummy\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe";

            Assembly asm = Assembly.LoadFile(asmPath);

            Type[] types = asm.GetTypes();
            foreach (Type t in types)
            {
                if (t.BaseType.FullName.Equals("System.Configuration.Install.Installer"))
                {
                    string servicePath = string.Format("/assemblypath={0}", asmPath);
                    string[] cmdLine = { servicePath };

                    InstallContext context = new InstallContext("", cmdLine);

                    System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
                    Installer install = (Installer)Activator.CreateInstance(t);
                    install.Context = context;
                    install.Install(state);
                }
            }
        }

Open in new window

0
 

Accepted Solution

by:
JimR123b earned 0 total points
ID: 34164229
With a stroke of luck, I've actually just managed to find my own solution:
AssemblyInstaller Installer = new AssemblyInstaller(AssemblyFilename, new string[] { });

Hashtable SavedState = new Hashtable();

Installer.Install(SavedState);

Installer.Commit(SavedState);









AssemblyInstaller Installer = new AssemblyInstaller(AssemblyFilename, new string[] { });

Hashtable SavedState = new Hashtable();

Installer.Uninstall(SavedState);

Open in new window

0
 

Author Closing Comment

by:JimR123b
ID: 34194925
Found my own solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now