问题
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.
回答1:
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.
回答2:
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
回答3:
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!
回答4:
System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");
This code help to you but you can get Access Denied.
For you to not get Access Denied:
- Right Click Project
- Add New İtem
- Add Application Manifest File
- Change this section
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
To this
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
回答5:
There are two ways to do this but fr both you need to run VS as administration.
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();
this code will also restart IIS and it will prompt CMD with few processing.
Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");
回答6:
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....
来源:https://stackoverflow.com/questions/7870745/how-can-i-restart-iis-from-c-sharp-code-running-as-a-user-who-is-an-administrato