views:

320

answers:

6

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

+2  A: 

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

Simon P Stevens
+2  A: 

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.

fretje
A: 

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

Dan Diplo
+2  A: 

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

Zensar
+3  A: 

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

monkey_p
I think 'w3svc' is better in this case, as that will only stop the web server. Stopping IISADMIN will also stop other services, like ftp and smtp.
fretje
yes, i think you correct, sorry, I don't have windows to check this
monkey_p
but the config file might still be locked by the admin service, you'll just have to check
monkey_p
+1 - using the service controller like this is a preferable way to control services than calling command line commands.
Rob Levine
+2  A: 

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.

RichardOD
or process explorer
fretje
Thanks- I'll update my answer.
RichardOD