请教C# Process启动CMD命令

heiyedebing 2013-01-29 08:31:50
一下代码为什么什么也没显示??(我是故意net use 一个无法登陆的电脑的),直接在CMD下使用会显示错误,而C#调用为什么没有显示,我应该如何判断有没有登陆成功
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
CMD cmd = new CMD();
string command = @"net use \\172.113.113.113 ""1234"" /user:""administrator""";
Console.WriteLine("CMD命令:"+command);
string result = cmd.CMDRun(command);
Console.WriteLine(result);
Console.ReadKey();
}
}
class CMD
{
public string CMDRun(string command)
{
Process p = new Process();
p.StartInfo.Arguments = "/c" + command;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();

string result = p.StandardOutput.ReadToEnd();
return result;
}
}
}
...全文
214 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
showjim 2013-01-30
  • 打赏
  • 举报
回复
既然设置了重定向
p.StartInfo.RedirectStandardOutput = true;
那么可以使用
process.StandardOutput.ReadToEnd();
  • 打赏
  • 举报
回复
引用 3 楼 g767160 的回复:
既然是错误,那就应该获取错误流,把 p.StandardOutput.ReadToEnd(); 换成这个 p.StandardError.ReadToEnd(); 就可以了, 要想两个都保留的话,可以换成这样 string result = p.StandardOutput.ReadToEnd(); if (result == "")……
应该是读取错误的时候搞错了。
祝福灵魂 2013-01-30
  • 打赏
  • 举报
回复
既然是错误,那就应该获取错误流,把 p.StandardOutput.ReadToEnd(); 换成这个 p.StandardError.ReadToEnd(); 就可以了, 要想两个都保留的话,可以换成这样 string result = p.StandardOutput.ReadToEnd(); if (result == "") { return p.StandardError.ReadToEnd(); } return result;
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
http://www.cnblogs.com/h2appy/articles/1204277.html

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧