【问题标题】:Executing command in command prompt with redirection on a C# .aspx page在 C# .aspx 页面上使用重定向在命令提示符下执行命令
【发布时间】:2015-08-28 16:29:53
【问题描述】:

我正在尝试在 .aspx 页面上以 C# 代码的命令提示符执行命令。该代码不会引发任何错误,但是,该命令不会执行,因为没有生成新文件。

我没有看到命令本身有任何问题,因为如果我从调试视图复制命令并将其粘贴到命令提示符中,它可以正常执行。

任何想法为什么我的代码不生成 ResultadoCheckMac_100939.txt?

代码:

        string cmd = ejecutable_CheckMac + " " + archivo_temporal + " > " + archivo_resultado;
        System.Diagnostics.Debugger.Log(0, null, "cmd: " + cmd);

        System.Diagnostics.Debugger.Log(0, null, "Start cmd execution.");
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = cmd;
        startInfo.CreateNoWindow = true; 
        startInfo.UseShellExecute = false;
        process.StartInfo = startInfo;
        process.Start();
        System.Diagnostics.Debugger.Log(0, null, "Cmd execution finished.");

调试视图输出:

00000014    0.00096980  [136] cmd: C:\inetpub\wwwroot\cgi-bin\tbk_check_mac.exe C:\inetpub\wwwroot\cgi-bin\log\DatosParaCheckMac_100939.txt > C:\inetpub\wwwroot\cgi-bin\log\ResultadoCheckMac_100939.txt   
00000015    0.00103170  [136] Start cmd execution.  
00000016    0.01946740  [136] Cmd execution finished.

【问题讨论】:

  • 你想要cmd /c <cmd>cmd /k <cmd>。您也可以针对ejecutable_CheckMac 执行并将其用作FileName 参数(然后在您的应用程序中重定向输出)。
  • IIS 用户是否对该目录有写权限?
  • 另外,Brad 是对的,> 重定向支持由 cmd.exe 提供,CreateProcess 不会自动提供它(这是 Process.Start()UseShellExecute = false 时使用的)跨度>
  • @BenVoigt 是的,读+写。
  • @BradChristie 抱歉,您的第一点是什么意思?我想要 cmd /c 而不是什么/在哪里?如果您提交答案,如果它解决了我的问题,我可以将其标记为正确。

标签: c# process cmd processstartinfo


【解决方案1】:

假设你想在 cmd 提示符下继续执行它,你可以这样做:

var psi = new ProcessStartInfo("cmd", "/c " + cmd); // note the /c
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;

Process.Start(psi);

/c 指示提示“执行以下字符串”。但是,更简洁的方法可能是拦截输出并自己捕获:

var psi = new ProcessStartInfo(ejecutable_CheckMac, archivo_temporal);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;

using (var proc = Process.Start(psi))
{
    using (StreamReader sr = proc.StandardOutput)
    {
        // This effectively becomes the intended contents
        // of `archivo_resultado`
        var summary = sr.ReadToEnd();
    }
}

示例

如果我想ping -n 1 8.8.8.8,我可以使用以下任一方法:

// Execute ping against command prompt
var psi = new ProcessStartInfo("cmd", @"/c ping -n 1 8.8.8.8 > save\to\ping.txt");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

Process.Start(psi);

或者,我可以这样做:

// Call ping directly passing parameters and redirecting output
var psi = new ProcessStartInfo("ping", "-n 1 8.8.8.8");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

psi.RedirectStandardOutput = true;
using (var proc = Process.Start(psi))
{
    using (StreamReader sr = proc.StandardOutput)
    {
        Console.WriteLine(sr.ReadToEnd());
    }
}

两者最终都给我(大致)相同的输出,只是现在我不必追踪文件。

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=18ms TTL=54

Ping statistics for 8.8.8.8:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 18ms, Maximum = 18ms, Average = 18ms

【讨论】:

  • 感谢您的解释。但是,使用这两种方法时,Process.Start(psi) 返回以下错误:无法使用实例引用访问成员“System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo)”;改为使用类型名称来限定它
  • 您是在尝试ProcessStart(),还是new Process.Start ()
  • 用 System.Diagnostics.Process.Start(startInfo) 解决了这个问题。谢谢。
  • 没问题,很乐意提供帮助。
猜你喜欢
  • 2011-10-26
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多