简体   繁体   English

如何从作为管理员用户运行的C#代码重新启动IIS?

[英]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. 通常(在Windows 7中),安装程序将要求修改系统的权限。 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. 我试图找出如何从作为AN管理员的用户运行的C#代码中采取管理员操作(重新启动IIS),而不是“管理员”帐户。

To run a process as elevated you can use the runas verb. 要以提升的方式运行进程,可以使用runas谓词。

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

For restarting IIS (as mentioned before) use iisreset. 要重新启动IIS(如前所述),请使用iisreset。

Hope you find this useful. 希望您觉得这个有帮助。

Try to execute the IISReset command from C# 尝试从C#执行IISReset命令

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

iisreset /noforce

Using ProcessStart 使用ProcessStart

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

If you're using AD Authentication and you're an administrator this should work 如果您使用的是AD身份验证,并且您是管理员,则应该可以使用

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. 这段代码对你有帮助,但你可以获得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 1)右键单击项目2)添加新的İtem3)添加应用程序清单文件在此处输入图像描述

4) change this code 4)更改此代码

<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. 有两种方法可以做到这一点,但你需要运行VS作为管理。

  1. This code will prompt in an empty cmd for some time and will close the window automatically. 此代码将在空cmd中提示一段时间,并将自动关闭窗口。

    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. 此代码也将重新启动IIS,它将提示CMD几乎没有处理。

      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 这是一个如何在电源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/ 另一种可能性是使用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/ 这是另一种直接在#http ://www.csharp-examples.net/restart-windows-service/的方式

I hope this helps.... 我希望这有帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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