?
Solved

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

Posted on 2010-11-18
11
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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