以编程方式重新启动IIS

以编程方式重新启动IIS

本文介绍了以编程方式重新启动IIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个C#/。NET应用程序重新启动IIS。这似乎是一个微不足道的问题,但我还没有成功至今,并没有一个答案,从<一个href="http://stackoverflow.com/questions/7870745/how-can-i-restart-iis-from-c-sharp-$c$c-running-as-a-user-who-is-an-administrato">this问题工作过。

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.

我已经试过这样:

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

不过,这将引发一个 Win32Exception - 找不到指定文件我也试着在文件名,并使用 UseShellExecute 但是没有这些选项帮助。

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();

和工作原理,但它给UAC提示,这点我不能因为这个应用程序将无需用户干预的情况下运行。

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?

推荐答案

你得到的最有可能的是,用户您要运行该应用程序不具有管理权限引起的异常。

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.

如果你运行你的应用程序作为管理员帐户,那么它应该自动启动IISRESET具有管理权限不UAC提示或错误。

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

这篇关于以编程方式重新启动IIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:47