我可以停止IIS吗?

the*_*ole 4 .net c# iis configuration

在.NET Windows应用程序中,以修改ASP.NET应用程序使用的远程计算机配置文件.但是,我一直收到错误:

System.IO.IOException: The process cannot access the file '[file name]' because it is being used by another process.

现在,这可能不是问题,但我想如果我可以停止IIS,那么我可以修改机器配置文件(没有得到异常),然后我可以使用以下代码重新启动IIS:

 Process proc = new Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.StartInfo.FileName = "iisreset";
            proc.StartInfo.Arguments = serverName;
            try
            {
                proc.Start();
                proc.WaitForExit();
                ...
Run Code Online (Sandbox Code Playgroud)

1)有没有办法在重新启动IIS的情况下停止IIS ,2)Doe这种更改server.config文件的方法是否有意义?

(注意,我正在使用正则表达式搜索和替换修改文件;这是一个问题吗?)

mon*_*y_p 8

你应该可以做这样的事情.我没有窗口,所以我无法检查服务的确切名称,但我认为它是"IISADMIN"或"w3svc".请记住,这应该是服务名称,而不是您在服务控制面板中看到的显示名称.

ServiceController controller  = new ServiceController();
controller.MachineName = "."; // or the remote machine name
controller.ServiceName = "IISADMIN"; // or "w3svc"
string status  = controller.Status.ToString();

// Stop the service
controller.Stop();

// Start the service
controller.Start();
Run Code Online (Sandbox Code Playgroud)

你也可以使用

net stop w3svc

要么

net stop IISADMIN

从命令行或代码中的过程中

  • 我认为'w3svc'在这种情况下更好,因为这只会停止Web服务器.停止IISADMIN也会停止其他服务,比如f​​tp和smtp. (2认同)