iisreset on remote machine (C#)

天涯浪子 提交于 2019-12-08 02:18:11

问题


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.


回答1:


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.




回答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";



回答3:


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

iisreset {servername}


来源:https://stackoverflow.com/questions/6675255/iisreset-on-remote-machine-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!