【问题标题】:COPY command can't find the path specified when run in C#COPY命令在C#中运行时找不到指定的路径
【发布时间】:2014-07-28 13:31:23
【问题描述】:

当我从命令行运行以下命令时,它工作正常。

COPY "C:\Windows\System32\winevt\Logs\Application.evtx" "C:\ProgramData\MyCompany\Support\Logs\Application.evtx"

但我想在 C# 中使用以下代码运行它

 System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + CurrentCommand);
 StreamReader.procStartInfo.RedirectStandardOutput = true;
 procStartInfo.RedirectStandardError = true;
 procStartInfo.UseShellExecute = false;

 // Do not create the black window.
 procStartInfo.CreateNoWindow = true;

 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.StartInfo = procStartInfo;
 proc.Start();

 while (!proc.StandardError.EndOfStream)
 {
      sError = proc.StandardError.ReadLine();
      //System.Windows.Forms.MessageBox.Show("ERROR: " + sError);
 }

 proc.WaitForExit();
 // Get the output into a string
 sResult = proc.StandardOutput.ReadToEnd();

sResult 返回以下错误:

系统找不到指定的路径

这是为什么?

【问题讨论】:

  • 你为什么做的这么复杂?? File.Copy(oldFile, newFile) 来自 System.IO 命名空间,你就完成了!
  • 因为从 ini 文件中读取的命令的完整列表都需要运行。所以我不知道最初它们只是传入的命令是什么。
  • 您能否将这些命令粘贴到mycommand.cmd 文件中,然后通过调用cmd /c mycommand.cmd 来运行它??

标签: c# command-line process command


【解决方案1】:

我猜您需要“转义”cmd /c 参数的命令参数,例如

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", CurrentCommand));

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2018-08-26
    • 2019-07-30
    • 2022-06-21
    相关资源
    最近更新 更多