执行位于远程计算机上的批处理文件

时间:2015-04-03 22:46:58

标签: c# batch-file

我尝试使用以下代码行执行位于远程计算机上的批处理文件:

System.Diagnostics.Process.Start(\\10.0.24.103\somePath\batchFile.bat);

它阻止了这行代码。当我尝试手动运行它(通过在Windows资源管理器中写入该地址)时,它可以工作,但我必须首先接受安全警告消息。我假设这就是它在通过代码完成时阻止的原因......有没有办法强迫它通过代码执行?

2 个答案:

答案 0 :(得分:1)

我通过向ProcessStartInfo对象添加更多详细信息来解决我的问题:

var process = new Process();

var startInfo = new ProcessStartInfo
{
  CreateNoWindow = true,
  FileName = "cmd.exe",
  Arguments = "/c \"\"" + batchFile + "\"\"",
  WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
  UseShellExecute = false,
  RedirectStandardError = true,
  RedirectStandardOutput = true
};

process.StartInfo = startInfo;
process.Start();
process.WaitForExit(30000);

我需要指定使用cmd.exe,以及在双引号中包含batchFile路径,以防路径中有空格。

答案 1 :(得分:0)

尝试使用cmd /c/c之后的空格)对其进行前缀。

此IP地址是否是您域中的Windows计算机等。