【发布时间】:2018-01-15 03:09:49
【问题描述】:
我有一个c#程序,目前我运行2个窗口,第一个是窗体窗口,第二个是用于调试的控制台窗口。
根据以下内容创建的控制台窗口:
Create a Windows Form project... Then: Project Properties -> Application -> Output Type -> Console Application
如何通过单击按钮从表单中关闭控制台窗口?
编辑:
我尝试执行以下操作,但这只是关闭表单窗口..
private void button1_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
编辑 2: 前面的代码只在下面的代码执行之前有效。
using (process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.WorkingDirectory = @"C:\";
process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
// Runs the specified command and exits the shell immediately.
//process.StartInfo.Arguments = @"/c ""dir""";
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Send a directory command and an exit command to the shell
process.StandardInput.WriteLine("cd " + currentPath);
process.StandardInput.WriteLine("ibt -mic");
}
【问题讨论】:
-
您尝试过什么,您有任何尝试过的代码吗?我们不会为您从头开始编写代码,但如果您向我们展示您目前所拥有的,您就更有可能得到回复。
-
请查看编辑
-
您发布的代码应该可以工作,它对我有用。你确定这是在执行吗?
-
@null:OP 并未尝试关闭发生
Click事件的程序。他们想关闭一个完全不同的程序。当然,Stack Overflow 上有很多问题和答案已经在讨论如何做到这一点(例如,将WM_CLOSE发布到程序中,或调用Process.Kill()) -
自从您上次编辑以来,可能发生的情况是您“关注”了 cmd 而不是实际的控制台,也许可以尝试关闭该进程或向其发送“退出”命令。