Restarting IIS programmatically

后端 未结 5 1116
闹比i
闹比i 2020-12-30 18:04

I need to restart IIS from a C#/.NET application. This seems like a trivial issue, but I haven\'t had success thus far, and none of the answers from this question have worke

相关标签:
5条回答
  • 2020-12-30 18:29

    FileName is wrong. try FileName = @"iisreset.exe"

    0 讨论(0)
  • 2020-12-30 18:30

    Right click on project name in Solution Explorer ->Add ->New Item-> Appliaction Manifest File.

    In it edit the

        <requestedPrivileges>
           <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
        </requestedPrivileges>
    

    this should solve your issue.

    0 讨论(0)
  • 2020-12-30 18:34

    The exception you're getting is most likely caused by the fact that the user you are trying to run this application as does not have administrative privileges.

    If you run your application as an administrator account then it should automatically launch iisreset with administrative privileges without a UAC prompt or an error.

    How you should go about running your process as an administrator is a separate issue. The most common way is to create an application manifest:

    http://msdn.microsoft.com/en-us/library/ms235229.aspx

    0 讨论(0)
  • 2020-12-30 18:39

    Alternatively (and cleaner) you could just use the ServiceController class to subsequently stop and start the iis service.
    You'll probably still need elevated privileges though... Impersonation might solve this; "impersonate an account with higher privileges." for restarting the service.

    A good example of how to start/stop (and restart) a windows service can be found here: Start, Stop and Restart Windows Service (C#)

    0 讨论(0)
  • 2020-12-30 18:51

    Another option to start the IIS:

    string serviceName = "W3SVC"; //W3SVC refers to IIS service
    ServiceController service = new ServiceController(serviceName);
    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running);// Wait till the service started and is running
    
    0 讨论(0)
提交回复
热议问题