【发布时间】: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