简体   繁体   中英

Can I stop an IIS?

In a .NET windows application to to modify a remote machine config file that is used by an ASP.NET application. However, I keep getting the error:

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

Now, this may not be the problem, but I'm figuring that if I can stop the IIS, then I can modify the machine config file (without getting the exception), and then I can restart the IIS using this code:

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

1) Is there a way to stop the IIS without restarting it, and 2) Doe this approach to changing the server.config file even make sense?

(note, I am modifying the file with regular expressions search and replace; is this a problem?)

You should be able to do something like this. I don't have windows, so I can't check the exact name of the service, but I think it is "IISADMIN" or "w3svc". Remember this should be the service name and not the display name you see in the service control panel.

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

You can also use

net stop w3svc

or

net stop IISADMIN

from the commandline or in your process in your code

Strange. A .config file should not be locked exclusively.
But to answer your question, you can also use the net command for this:

net stop w3svc

to stop the www service, and

net start w3svc

to start it again.

You can also do this programmatically as described by @monkeyp

Note that I would advice against this and first try to determine (and resolve) the cause of the lock as described by @RichardOD.

You can use the IISRESET /STOP command.

If you type IISRESET /? you will get a list of other available options.

[Edit: Pass the "/STOP" switch as the arguments property on the process' startinfo object.]

应该是“iisreset / STOP”来停止服务,然后“iisreset / START”重启它们。

Use a tool like wholockme or unlocker to find the root cause of the locking.

Update- another option is to use Process Explorer (thanks fretje)- this is a good option as lots of developers have this utility on their PC.

您通常可以只回收或停止/启动IIS正在运行的应用程序池 ,而不是完全重新启动IIS。

Using System.Diagnostics;

//to stop ISS
ProcessStartInfo startInfo = new ProcessStartInfo("iisreset.exe", " /stop");
System.Diagnostics.Process.Start(startInfo);

//to start ISS
ProcessStartInfo stopInfo = new ProcessStartInfo("iisreset.exe", " /start");
System.Diagnostics.Process.Start(stopInfo);

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