C# 在cmd.exe运行时加载工具栏
我想做什么:C# 在cmd.exe运行时加载工具栏,c#,winforms,cmd,progress-bar,C#,Winforms,Cmd,Progress Bar,我想做什么: Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.WorkingDirectory = @"C:\" p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:\"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(Command_That's_Called);
p.WaitForExit(); // still hangs
将命令传递给.cmd,在命令执行时显示加载栏,退出cmd并在进度栏已满后显示消息框
发生了什么:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:\"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(Command_That's_Called);
p.WaitForExit(); // still hangs
当我单击发送命令的按钮时,应用程序挂起,命令被执行,但CMD在完成后永远不会退出,因此应用程序保持冻结状态(直到我手动关闭CMD.exe)。我也不知道在命令执行时如何显示加载条。当加载栏已满时,我将显示消息框
我的代码:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:\"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(Command_That's_Called);
p.WaitForExit(); // still hangs
^在按钮点击事件中执行
我尝试过的事情:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:\"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(Command_That's_Called);
p.WaitForExit(); // still hangs
还有线程,但我得到了一个错误,比如“从创建它的线程以外的线程访问”
关于CMD不关闭,我会在一段时间后杀死它,但是命令完成的时间长短取决于各种因素 要在命令执行完毕后退出CMD进程,请尝试在进程参数的开头添加“/C”:
p.StartInfo.Arguments = "/C (your arguments)";
如果在命令提示符中键入“cmd/?”,您会发现“/C:执行字符串指定的命令,然后终止”
要添加加载栏,您需要了解重定向输出时的相关信息,您还必须阅读它。如果你不这样做,程序就会卡住。我该怎么做?此时输出被读入文本框,但它仍然挂起。在命令中添加退出代码或在程序中关闭它。进度条可能是您可以打开一个新线程并监视cmd.exe,但是显示的进度数字是个问题,因为你实际上不知道需要多长时间。我建议你可以使用一个永无止境的进度算法,开始时速度很快,但会变得越来越慢,如果不将其设置为“结束”,它将永远不会结束。在命令完成后,我应该使用什么代码退出CMD?至于加载栏,我可以从CMD输出结果,完成后显示messagebox,但我仍然不知道如何在cmd执行完命令后关闭它。使用“/C我的脚本”使它工作,现在要尝试处理加载栏,谢谢!