Solved

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

Posted on 2010-11-18
11
355 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

746 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

12 Experts available now in Live!

Get 1:1 Help Now