1

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'.

Community
  • 1
  • 1
tec
  • 999
  • 3
  • 18
  • 40
  • You've said it doesnt work, but what about it isnt working... – BugFinder Nov 29 '16 at 09:05
  • The printing of the result ('false') is not working, either in the console either in the debug console – tec Nov 29 '16 at 09:06
  • There are some known issues with the process input/output under debug.. Does it work under non debug conditions? – BugFinder Nov 29 '16 at 09:08
  • PS - it doesnt help that server+"(net has no space before the ( – BugFinder Nov 29 '16 at 09:15
  • When I added the space before the ( I got a completely different result .... – BugFinder Nov 29 '16 at 09:15
  • @BugFinder Tested with space, not working. In cmd I'm getting the same reult with or without space: 'false'. I think I'm missing something about how the hell works the console on SharpDevelop.. – tec Nov 29 '16 at 09:17
  • The added space also worked for me; check my last edit... – Adam Calvet Bohl Nov 29 '16 at 09:19
  • I notice that you are redirecting all of the standard, error, and input streams. Check the docs for the `ProcessStartInfo` class as there is a potential deadlock problem that might prevent you from getting any output. – Chris Dunaway Nov 29 '16 at 22:04
  • @ChrisDunaway Thx anyway. I asked the same topic again and got the solution. http://stackoverflow.com/questions/40869169/process-class-not-printing-echo-output-c-sharp – tec Nov 29 '16 at 22:13

2 Answers2

0

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)");
Community
  • 1
  • 1
Adam Calvet Bohl
  • 1,009
  • 14
  • 29
0

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
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Copy paste from my code: proc.StandardInput.WriteLine(" if exist "+ **server+" (net ** use "+driveChar+": "+server+" /user:"+user+" "+password+" ) else (echo false)"); I have the space! :) But no output from the command either in the Console, either in the Output Debug console. The ** means bold. – tec Nov 29 '16 at 09:37
  • It has to be something about SharpDevelop console – tec Nov 29 '16 at 09:40
  • Then take it up with them at a guess. – BugFinder Nov 29 '16 at 10:20
  • I've used SharpDevelop and worked for me... Why don't you test something like 'if exist \qwerty (echo true) else (echo false)'? – Adam Calvet Bohl Nov 29 '16 at 10:34
  • Neither works (it does on cmd). Do I have to do something in order to use the console In SharpDevelop? I added Console.Read(); at the end of mapDrive() to block the console. But nothing shows up. – tec Nov 29 '16 at 10:43
  • What do you mean with 'run a dir', the drive I want to map it's already a directory. – tec Nov 29 '16 at 10:50
  • dir, dos command, lists files.. would give you output if the drive maps as your script doesnt show anything if your drive is mapped – BugFinder Nov 29 '16 at 11:07
  • How did you add the dir?? – BugFinder Nov 29 '16 at 11:28
  • proc.StandardInput.WriteLine(" if exist "+server+" (dir t: ) else (echo false)"); Works in cmd. I will delete the question today, it's extending so much. I will investigate by myself. – tec Nov 29 '16 at 11:30