3
Process myProcess = new Process();
ProcessStartInfo remoteAdmin =
            new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\iisreset.exe /restart");

remoteAdmin.UserName = username;
remoteAdmin.Password = pwd;
remoteAdmin.Domain = domain;
myProcess.StartInfo = remoteAdmin;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;

myProcess.Start();   --- ERROR HERE

Can not find the file specified.

But when I try to run iisreset on the local machine by cmd it's working.

Draken
  • 3,134
  • 13
  • 34
  • 54
Pavel Gr.
  • 141
  • 2
  • 9

3 Answers3

5

Unless I'm missing something, (Environment.GetFolderPath(Environment.SpecialFolder.System) will get back the local machine (Where the code is running) special folder. So it's expecting the file C:\Windows\System\iisreset.exe to be located on your machine. The only method I could see to get around this, is to drop the C:\ and instead add in the device's name \\DeviceName\C$\ and then the filepath. This is assuming the special folder system is located in the same place on your machine and the remote machine.

The only other method, to get the remote machines system directory is to get it via WMI or via a reg entry reading.

So if using WMI:

"SELECT * FROM Win32_OperatingSystem"

Once done, you would then need to build the folder string yourself from that.

Draken
  • 3,134
  • 13
  • 34
  • 54
2

There is no file called C:\Windows\System\iisreset.exe /restart (assuming that Environment.GetFolderPath(Environment.SpecialFolder.System) returns C:\Windows\System\

So you would want

ProcessStartInfo remoteAdmin = 
     new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "iisreset.exe");
remoteAdmin.Arguments = "/restart";

But Environment.GetFolderPath(Environment.SpecialFolder.System) probably returns something like C:\Windows\System (note no trailing slash), and there is definitely no file called c:\windows\systemiisreset.exe

So you would actually want

ProcessStartInfo remoteAdmin = 
    new ProcessStartInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "iisreset.exe"));
remoteAdmin.Arguments = "/restart";
Jon
  • 16,212
  • 8
  • 50
  • 62
  • i tried this, and check the remoteAdmin.FileName on breackpoint it "c:\\windows\\system\\iisreset.exe". but myProcess.Start(); still the same error – Pavel Gr. Jul 14 '11 at 10:19
  • have you verified that the file exists in that location on the server? (it probably should). Does the ASP.NET worker process have permission to run it? – Jon Jul 14 '11 at 10:39
  • new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "iisreset.exe"); this code try to run iisreset on my local machine(on my machine it not exist). but Question, why its heppen, why not on remote machine? i`m a littel confused( – Pavel Gr. Jul 14 '11 at 13:21
0

iisreset.exe supports remote calls, so instead of using WMI to get remote directory you can actually just do:

iisreset {servername}
Alex Lapa
  • 1,149
  • 11
  • 21
  • Flagged up in the lqp queue. Alexander, can you flesh this answer out a bit more to explain what you're suggesting? – Gayot Fow Oct 07 '13 at 22:56