【问题标题】:Why Process not printing output from cmd C#?为什么进程不打印 cmd C# 的输出?
【发布时间】:2016-11-29 08:36:25
【问题描述】:

我正在尝试从 cmd 获取输出,此命令在命令行中运行良好: 如果存在 \qwerty (net use T: \querty ) else (echo false) 但是当我从 c# 这样做时不起作用。这里的方法:

void mapDrive(String driveChar, string server,string user, string password){

    try
    {               
        ProcessStartInfo procStartInfo;
        procStartInfo=new ProcessStartInfo();
        procStartInfo.FileName = @"C:\windows\system32\cmd.exe";
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.ErrorDataReceived += cmd_Error;
        proc.OutputDataReceived += cmd_DataReceived;
        proc.EnableRaisingEvents = true;
        proc.Start();
        proc.BeginOutputReadLine();
        proc.BeginErrorReadLine();

        proc.StandardInput.WriteLine(" if exist "+server+"(net use "+driveChar+": "+server+" /user:"+user+" "+password+" ) else (echo false)");
        //it should print 'false'
        proc.WaitForExit();     

    }
    catch (Exception e)
    {
        // MessageBox.Show(e.Message);
    }
}

static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("Output from other process");
    Console.WriteLine(e.Data);
}

static void cmd_Error(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("Error from other process");
    Console.WriteLine(e.Data);
}

我从here获取代码

编辑: 将“调试”替换为“控制台”。

【问题讨论】:

  • 你说不行,那不行怎么办……
  • 打印结果('false')不起作用,无论是在控制台还是在调试控制台中
  • debug 下的进程输入/输出存在一些已知问题。它在非调试条件下是否工作?
  • PS - 它没有帮助 server+"(net 在 (
  • 当我在 ( 之前添加空格时,我得到了完全不同的结果......

标签: c# printing cmd output


【解决方案1】:

如果您的应用程序是“命令行应用程序”,则应使用Console.WriteLine

见:

https://msdn.microsoft.com/en-us/library/zdf6yhx5(v=vs.110).aspx

还有

What's the difference between Console.WriteLine() vs Debug.WriteLine()?

编辑:再次检查原始代码...它已经在使用Console.WriteLine

编辑2: 我试过这个并为我工作:

static void mapDrive(String driveChar, string server,string user, string password){

    try
    {               
        ProcessStartInfo procStartInfo;
        procStartInfo=new ProcessStartInfo();
        procStartInfo.FileName = @"C:\windows\system32\cmd.exe";
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.ErrorDataReceived += cmd_Error;
        proc.OutputDataReceived += cmd_DataReceived;
        proc.EnableRaisingEvents = true;
        proc.Start();
        proc.BeginOutputReadLine();
        proc.BeginErrorReadLine();

        proc.StandardInput.WriteLine(" if exist v: (echo true) else (echo false)");
        //it should print 'false'
        proc.WaitForExit();     

    }
    catch (Exception e)
    {
        // MessageBox.Show(e.Message);
    }
}

static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("Output from other process");
    Console.WriteLine(e.Data);
}

static void cmd_Error(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("Error from other process");
    Console.WriteLine(e.Data);
}

Edit3:好的,我想我找到了: 尝试在 '"+server+"' 和 '(net' 之间添加一个空格...:

proc.StandardInput.WriteLine(" if exist "+server+" (net use "+driveChar+": "+server+" /user:"+user+" "+password+" ) else (echo false)");

【讨论】:

    【解决方案2】:

    当我进行一次更改时,您的代码对我来说运行良好

    proc.StandardInput.WriteLine(" if exist "+server+" (net use "+driveChar+": "+server+" /user:"+user+" "+password+" ) else (echo false)");
    

    如果你在 ( 前面放一个空格,它会按预期返回

    Output from other process
    Microsoft Windows [Version 6.1.7601]
    Output from other process
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    Output from other process
    
    Output from other process
    C:\Users\me\Documents\Visual Studio 2015\Projects\ConsoleApplication2\Conso
    leApplication2\bin\Debug> if exist \\server01\share (net use Z: \\server01\share
     /user:. . ) else (echo false)
    Output from other process
    false
    Output from other process
    

    【讨论】:

    • 从我的代码中复制粘贴:proc.StandardInput.WriteLine(" if exists "+ **server+" (net ** use "+driveChar+": "+server+" /user:"+user+" "+password+" ) else (echo false)");我有空间! :) 但无论是在控制台中,还是在输出调试控制台中,命令都没有输出。 ** 表示粗体。
    • 它必须与 SharpDevelop 控制台有关
    • 那就和他们一起猜吧。
    • 我使用了 SharpDevelop 并为我工作......你为什么不测试类似'if exists \qwerty (echo true) else (echo false)'之类的东西?
    • 两者都不起作用(它在 cmd 上起作用)。我必须做些什么才能在 SharpDevelop 中使用控制台吗?我添加了 Console.Read();在 mapDrive() 结束时阻止控制台。但什么也没有出现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多