【发布时间】: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 在 (
-
当我在 ( 之前添加空格时,我得到了完全不同的结果......