为什么Process不能从cmd C#打印输出?

时间:2016-11-29 08:36:26

标签: c# printing cmd output

我要从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

获取了代码

编辑: 取代'调试' FOR' Console'。

2 个答案:

答案 0 :(得分:0)

如果您的应用程序是"命令行应用程序",则应使用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

<强> EDIT2: 我试过这个并为我工作:

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);
}

编辑3:好的,我想我已经找到了: 尝试在&#39;&#34; +服务器+&#34;&#39;之间添加空格。和&#39;(网&#39; ...:

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

答案 1 :(得分:0)

您的代码对我来说没问题,正如您在进行一次更改时所期望的那样

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