thewillcole
thewillcole

Reputation: 3045

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

Upvotes: 5

Views: 7535

Answers (7)

David Castro
David Castro

Reputation: 1975

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

Upvotes: 3

fretje
fretje

Reputation: 8382

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.

Upvotes: 4

RichardOD
RichardOD

Reputation: 29157

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.

Upvotes: 2

monkey_p
monkey_p

Reputation: 2879

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

Upvotes: 9

Zensar
Zensar

Reputation: 817

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

Upvotes: 2

Dan Diplo
Dan Diplo

Reputation: 25349

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

Upvotes: 0

Simon P Stevens
Simon P Stevens

Reputation: 27509

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

Upvotes: 2

Related Questions