Solved

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

Posted on 2010-11-18
11
359 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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