Tutorial :Can I stop an IIS?



Question:

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?)


Solution:1

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


Solution:2

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.


Solution:3

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.]


Solution:4

Should be "iisreset /STOP" to stop the services, then "iisreset /START" to restart them.


Solution:5

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.


Solution:6

You can often just recycle or stop/start the Application Pool IIS is running, rather than restarting IIS altogether.


Solution:7

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »