如何在c#中获取执行命令window
How to get execution command window in c#
我从我的 C# 应用程序执行命令。
它运行良好,但有时会出错。
我的问题是我看不到错误或命令中的任何其他文本 window。
只是一片空白。
有什么方法可以使文本在执行时显示的时间与此处显示的相同?
这是我的代码:
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
//processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.WorkingDirectory = workingFolder;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);//MessageBox.Show(e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);//MessageBox.Show(e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
我不是这方面的专家,但我认为您可以在 try-catch
块中编写此代码,并使用 Console.WriteLine()
命令在屏幕上显示异常消息。
在您的第一个屏幕截图中,cmd
window 的标题栏中有 Select
。
您是否知道程序随后暂停的事实?
如果您单击 window 会出现 Select
,并且可以通过按 Enter
继续(如果我没记错的话)。
如果你使用 Console.WriteLine()
并且 cmd
window 没有暂停,你应该看到你写了什么。
你的代码没有问题,问题是你运行你的程序路径错误。
按照以下步骤找到您的应用的路径:
然后,在 cmd.exe
中转到带有一堆 cd
命令的路径。
这是代码:
var command = "echo hello world"; // < ------ example
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
//processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
输出:
output>>hello world
output>>
error>>
ExitCode: 0
Press any key to continue . . .
此外,您可以使用 Ctrl + F5
运行 您的应用
我知道这些很明显,但值得一提。
更新
你应该指定一个命令,也许你没有设置任何命令或者你的命令~没有输出~
更新2
我更改代码,当用户将 args
发送到 myapp.exe
时,它会将其定向到 运行。
static void Main(string[] args)
{
var command = string.Join("", args);
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
...
输出:
C:\Users\Mahan\Source\Repos\ConsoleApp11\ConsoleApp11\bin\Debug>
myapp.exe echo hello
output>>hello world
output>>
error>>
ExitCode: 0
我从我的 C# 应用程序执行命令。
它运行良好,但有时会出错。
我的问题是我看不到错误或命令中的任何其他文本 window。
只是一片空白。
有什么方法可以使文本在执行时显示的时间与此处显示的相同?
这是我的代码:
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
//processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.WorkingDirectory = workingFolder;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("output>>" + e.Data);//MessageBox.Show(e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
Console.WriteLine("error>>" + e.Data);//MessageBox.Show(e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
我不是这方面的专家,但我认为您可以在 try-catch
块中编写此代码,并使用 Console.WriteLine()
命令在屏幕上显示异常消息。
在您的第一个屏幕截图中,cmd
window 的标题栏中有 Select
。
您是否知道程序随后暂停的事实?
如果您单击 window 会出现 Select
,并且可以通过按 Enter
继续(如果我没记错的话)。
如果你使用 Console.WriteLine()
并且 cmd
window 没有暂停,你应该看到你写了什么。
你的代码没有问题,问题是你运行你的程序路径错误。
按照以下步骤找到您的应用的路径:
然后,在 cmd.exe
中转到带有一堆 cd
命令的路径。
这是代码:
var command = "echo hello world"; // < ------ example
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
//processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
输出:
output>>hello world output>> error>> ExitCode: 0 Press any key to continue . . .
此外,您可以使用 Ctrl + F5
运行 您的应用
我知道这些很明显,但值得一提。
更新
你应该指定一个命令,也许你没有设置任何命令或者你的命令~没有输出~
更新2
我更改代码,当用户将 args
发送到 myapp.exe
时,它会将其定向到 运行。
static void Main(string[] args)
{
var command = string.Join("", args);
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
...
输出:
C:\Users\Mahan\Source\Repos\ConsoleApp11\ConsoleApp11\bin\Debug>
myapp.exe echo hello
output>>hello world output>> error>> ExitCode: 0