简体   繁体   中英

How can I restart IIS from C# code running as a user who is an administrator?

Typically (in Windows 7), installing a program will ask for permission to modify the system. As an administrator, I can give the authorization without supplying a password.

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

To run a process as elevated you can use the runas verb.

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

For restarting IIS (as mentioned before) use iisreset.

Hope you find this useful.

Try to execute the IISReset command from 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're using AD Authentication and you're an administrator this should work

For anyone still looking for this, here is code that I use to help me out 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!

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

this code help to you but you can get Access Denied.

For your not get Access Denied ;

1)Right Click Project 2)Add New İtem 3)Add Application Manifest File enter image description here

4) change this code

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

as this

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

There are two ways to do this but fr both you need to run VS as administration.

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

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

    1. this code will also restart IIS and it will prompt CMD with few processing.

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

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

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

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

I hope this helps....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM