为什么处理打印输出从cmd C#? [英] Why Process not printing output from cmd C#?

查看:197
本文介绍了为什么处理打印输出从cmd C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从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;

进程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 exists+ server +(net use+ driveChar +:+ server +/ user:+ user ++ password + );
//应该打印'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);
}

我从这里



编辑:
'Debug'FOR'Console'。

解决方案

您的代码对我来说是正常的, / p>

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

如果您在前面放置一个空格(按预期返回)



其他进程的输出
Microsoft Windows [版本6.1.7601]
其他进程的输出
版权所有(c)2009 Microsoft Corporation。保留所有权利
其他进程的输出

其他进程的输出
C:\Users\me\Documents\Visual Studio 2015 \Projects \ConsoleApplication2 \Conso
leApplication2 \bin\Debug>如果存在\\server01\share(net use Z:\\server01\share
/ user :. )else(echo false)
其他进程的输出
false
其他进程的输出


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

解决方案

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

这篇关于为什么处理打印输出从cmd C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆