1

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 worked.

I am a local administrator on the machine.

I've tried this:

var process = new Process
{
   StartInfo =
   {
       Verb = "runas",
       WorkingDirectory = @"C:\Windows\System32\",
       FileName = @"issreset.exe"
   }
};
process.Start();

but this throws a Win32Exception - cannot find the file specified. I've also tried various combinations of putting the whole path in FileName, and using UseShellExecute but neither of those options helped.

I've also tried invoking it via the command line:

var process = new Process
{
   StartInfo =
   {
       Verb = "runas",
       WindowStyle = ProcessWindowStyle.Hidden,
       FileName = @"cmd.exe",
       Arguments = "/C iisreset"
   }
};
process.Start();

and this works, but it gives a UAC prompt, which I cannot have as this application will be running without user intervention.

Is there anything else I could try?

Community
  • 1
  • 1
MgSam
  • 12,139
  • 19
  • 64
  • 95
  • I think you want `iisreset.exe`, not `iisreset`. You need the file name extension. – Trevor Elliott Sep 30 '13 at 20:55
  • You're right. That was a typo on my part in writing the question. I have tried that. – MgSam Sep 30 '13 at 20:56
  • 3
    The first will work if you set `UseShellExecute` to true. Otherwise, `FileName` has to be the full path. There is no way to bypass the UAC, which is kinda the point. You could turn it off on that machine. – cadrell0 Sep 30 '13 at 20:58
  • Your first snippet of code works fine for me. Are you sure that's the code you've tried? Are you sure IIS is installed and the file is there? – Trevor Elliott Sep 30 '13 at 20:59
  • 1
    This doesnt bypass UAC, but it only prompts for a password: `runas /user:Administrator iisreset` as an argument to your second process. – crthompson Sep 30 '13 at 20:59
  • You've got `"issreset.exe"`, it should be `"iisreset.exe"`. (iss != iis) – intrepidis Sep 18 '19 at 12:00

5 Answers5

4

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.

Kishan
  • 111
  • 7
2

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#)

DogGuts
  • 300
  • 2
  • 9
  • What would the name of the IIS service be? I don't see it in Task Manager -> Services tab in Windows 8. – Zack Aug 01 '14 at 18:21
  • I found out it is "w3svc" from this blog post http://mphokazito.wordpress.com/2011/02/07/how-to-restart-iis-using-c/ – Zack Aug 01 '14 at 18:33
2

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

Mustafa Gülmez
  • 125
  • 1
  • 6
1

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

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
0

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
Shiko
  • 2,448
  • 1
  • 24
  • 31