How can I restart IIS from C # code running as an admin user?

Typically (on Windows 7) installing a program will require permission to modify the system. As an administrator, I can give permission without providing a password.

I am trying to figure out how to take an administrator action (restart IIS) from C # code running as a user who is an AN administrator, but not an "Administrator" account.

+8
source share
6 answers

To start the process as elevated, you can use the verb runas.

Process elevated = new Process(); elevated.StartInfo.Verb = "runas"; elevated.StartInfo.FileName = "Whatever.exe"; elevated.Start(); 

To restart IIS (as mentioned earlier) use iisreset.

I hope you find this helpful.

+4
source

Try running IISReset command with C #

http://technet.microsoft.com/en-us/library/cc758159(WS.10).aspx

 iisreset /noforce 

Using ProcessStart

 System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe"); 

If you use AD Authentication and you are an administrator, this should work

+3
source

For those who are still looking for this, here is the code I use to help me with this.

  private static void DoIISReset() { Process iisReset = new Process(); iisReset.StartInfo.FileName = "iisreset.exe"; iisReset.StartInfo.RedirectStandardOutput = true; iisReset.StartInfo.UseShellExecute = false; iisReset.Start(); iisReset.WaitForExit(); } 

Hope this helps!

+2
source
 System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe"); 

This code will help you, but you may be denied access.

For you to not access denied:

  1. Right click project
  2. Add New Item
  3. Add application manifest file
  4. Change this section

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To that

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

+2
source

There are two ways to do this, but for both you need to run VS as administrator.

  1. This code will be displayed in an empty cmd for some time and will automatically close the window.

    Process iisReset = new Process(); iisReset.StartInfo.FileName = "iisreset.exe"; iisReset.StartInfo.RedirectStandardOutput = true; iisReset.StartInfo.UseShellExecute = false; iisReset.Start(); iisReset.WaitForExit();

    1. this code also restarts IIS and requests CMD with little processing.

      Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");

0
source

Here is a link to how this is done in the power shell http://www.computerperformance.co.uk/powershell/powershell_service_start.htm

Another possibility is to use WMI http://www.motobit.com/tips/detpg_vbs-wmi-restart-service/

Here is another way right at # http://www.csharp-examples.net/restart-windows-service/

Hope this helps ...

-one
source

All Articles