执行cmd然后卡住了,怎么办呀?谢谢了,大家帮忙看下.

「已注销」 「已注销」 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如果执行cmd或者command这些命令然后就会在p.StandardOutput.ReadToEnd();这里卡住...
现在我解决的方法就是


public static string executeCmd(string command)
{
string strRst = "0";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine("@echo off");
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");

System.Timers.Timer timer = new System.Timers.Timer(3000);
timer.Start();
timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{

p.CloseMainWindow();
timer.Enabled = false;
timer.Close();
};

strRst = p.StandardOutput.ReadToEnd();
timer.Enabled = false;
timer.Close();
strRst = strRst.Substring(strRst.IndexOf(command) + command.Length);
strRst = strRst.Substring(0,strRst.Length - 8);
if (p.StandardError.ReadToEnd() != "")
{
strRst = p.StandardError.ReadToEnd();
}

p.WaitForExit();
p.Close();
return strRst;
}


这样加个时间,一旦3秒后强行结束,但是p.StartInfo.CreateNoWindow = true;就不管用了,
p.StartInfo.CreateNoWindow = false;就可以..
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
「已注销」
C# 111,129 成员 642,539 内容