Why Process not printing output from cmd C#?

2019-07-29 22:24发布

问题:

I'm tring to get the output from cmd, this command is working fine in the command line: if exist \qwerty (net use T: \querty ) else (echo false) But when I do that from c# doesn't work. Here the methods:

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

I took the code from here

EDIT: Replaced 'Debug' FOR 'Console'.

回答1:

If your application is a "command line application", you should use Console.WriteLine.

See:

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

and also

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

Edit: check the original code again... it is already using Console.WriteLine.

Edit2: I tried this and worked for me:

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: Ok, I think I've found it: Try adding a space between '"+server+"' and '(net'...:

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


回答2:

Your code worked fine for me as you would have expected when I made one change

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

If you put a space in front of ( it came back as expected

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