Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to start a windows service not installed on computer via asp.net with c#?I am able to start the service installed in system.
My code as follows:
Under web.config
C#
<identity impersonate="true">
     userName="Domain/Username" password="password" /></identity>


Under aspx.cs page
C#
private void StartService(string serviceName, int timeoutMilliseconds)
    {
        Configuration objConfigFile;
        //getting an instance of the configuration file
        objConfigFile = WebConfigurationManager.OpenWebConfiguration(
                               HttpContext.Current.Request.ApplicationPath);

        //getting an instance of the "identity" section of the cofiguration file
        IdentitySection objIdentitySection =
          (IdentitySection)objConfigFile.GetSection("system.web/identity");

        if (objIdentitySection != null)
        {
            string username = objIdentitySection.UserName;
            string password = objIdentitySection.Password;
            bool impersonate = objIdentitySection.Impersonate;
            //Configuration currentConfiguration = objIdentitySection.C.CurrentConfiguration;

            //Obviously you won't be doing this. The lines below are just for testing purpose
         

            ServiceController service = new ServiceController(serviceName);
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch(Exception ex)
            {
                throw (ex);
            }
        }


    }
Posted
Updated 8-Jan-14 23:09pm
v2
Comments
Bojjaiah 9-Jan-14 5:08am    
always use pre tags when post a question/answer.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900