none
Install,UnInstall,Start And Stop A Windows Service RRS feed

  • Question

  • i Want To Install,Un Install,Start And Stop A Windows Service using A Windows Application kindly help me.. :-)
    Thursday, March 13, 2014 7:58 AM

Answers

  • Hi,
    It seems that you want to Install,Unstall,Star and Stop a windows service Programmatically. Then please try to check the following:

    Install Service:

    You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters:

    /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
                if (System.Environment.UserInteractive)
                {
    
                    if (args.Length > 0)
                    {
                        switch (args[0])
                        {
                            case "-install":
                                {
                                    ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                                    break;
                                }
                            case "-uninstall":
                                {
                                    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                                    break;
                                }
                        }
                    }
                }
                else
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[] { new MyService() };
                    ServiceBase.Run(ServicesToRun);
                }
            }

    Uninstall Service:

    You can use the ServiceInstaller.Uninstall method in System.ServiceProcess.dll.  For example:

    ServiceInstaller ServiceInstallerObj = new ServiceInstaller(); 
    InstallContext Context = new InstallContext("<<log file path>>", null); 
    ServiceInstallerObj.Context = Context; 
    ServiceInstallerObj.ServiceName = "MyService"; 
    ServiceInstallerObj.Uninstall(null); 

    This method will attempt to stop the service first before uninstalling.

    Start and Stop Service:

    var sc = new System.ServiceProcess.ServiceController("MyService", "MyRemoteMachine");
    sc.Start();
    sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
    sc.Stop();
    sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, March 14, 2014 6:29 AM
    Moderator