远程计算机上的IIS设置(C#)

远程计算机上的IIS设置(C#),c#,iisreset,C#,Iisreset,找不到指定的文件 但是当我尝试通过cmd在本地计算机上运行iisreset时,它正在工作。没有名为C:\Windows\System\iisreset.exe/restart(假设Environment.GetFolderPath(Environment.SpecialFolder.System)的文件返回C:\Windows\System\ 所以你会想要 Process myProcess = new Process(); ProcessStartInfo remoteAdmin =

找不到指定的文件


但是当我尝试通过cmd在本地计算机上运行iisreset时,它正在工作。

没有名为
C:\Windows\System\iisreset.exe/restart
(假设
Environment.GetFolderPath(Environment.SpecialFolder.System)
的文件返回
C:\Windows\System\

所以你会想要

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
但是
Environment.GetFolderPath(Environment.SpecialFolder.System)
可能返回类似
C:\Windows\System
(注意没有尾随的斜杠),并且肯定没有名为
C:\Windows\systemiisreset.exe的文件

所以你真的想要

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

除非我遗漏了什么,
(Environment.GetFolderPath(Environment.SpecialFolder.System)
将返回本地计算机(代码运行的地方)特殊文件夹。因此它希望文件
C:\Windows\System\iisreset.exe
位于您的计算机上。我能找到的唯一解决方法是删除
C:\
,而是添加设备名
\\DeviceName\C$\
,然后添加文件路径。这是假设特殊文件夹系统位于您的计算机和远程计算机上的相同位置

获取远程机器系统目录的唯一其他方法是通过WMI或reg条目读取

因此,如果使用WMI:

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

完成后,您将需要自己从中构建文件夹字符串。

iisreset.exe支持远程调用,因此,您实际上只需执行以下操作,而不是使用WMI获取远程目录:

"SELECT * FROM Win32_OperatingSystem"

我尝试了这个,并检查了breackpoint上的remoteAdmin.FileName“c:\\windows\\system\\iisreset.exe”。但是myProcess.Start();仍然是相同的错误您是否验证了该文件存在于服务器上的该位置?(它可能应该存在)。ASP.NET工作进程是否有权运行它?新建ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System)+“iisreset.exe”);这段代码试图在我的本地机器上运行iisreset(在我的机器上它不存在)。但问题是,为什么它是hepen,为什么不在远程机器上?我有点困惑(在lqp队列中标记)。Alexander,你能把这个答案细化一点,解释一下你的建议吗?
iisreset {servername}