Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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

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
JimR123b
Asked:
JimR123b
  • 6
  • 5
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JimR123bAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JimR123bAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JimR123bAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JimR123bAuthor Commented:
...and how do I do that please?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JimR123bAuthor Commented:
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
 
JimR123bAuthor Commented:
Found my own solution
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now