![]() |
苦闷的奔马 · Xcode打印不出任何东西 || ...· 3 周前 · |
![]() |
眼睛小的乌冬面 · vue 页面跳转之参数传递与接收参数 - 掘金· 1 年前 · |
![]() |
不要命的西装 · 关于thymeleaf模板引擎中th:if的 ...· 1 年前 · |
![]() |
另类的钢笔 · 【沒錢買ps,PyQt自己寫】Day 9 ...· 1 年前 · |
![]() |
不要命的野马 · python - ...· 1 年前 · |
有没有办法在执行控制台应用程序时隐藏控制台窗口?
我当前正在使用Windows窗体应用程序启动控制台进程,但我不希望在任务运行时显示控制台窗口。
如果使用的是
ProcessStartInfo
类,则可以将窗口样式设置为hidden -如果是控制台(而非图形用户界面)应用程序,则必须将CreateNoWindow设置为
true
System.Diagnostics.ProcessStartInfo start =
new System.Diagnostics.ProcessStartInfo();
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hides GUI
start.CreateNoWindow = true; //Hides console
您可以使用 FreeConsole 接口将控制台与进程分离:
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
(当然,这只有在您有权访问控制台应用程序的源代码时才适用)
如果您正在创建一个不需要用户输入的程序,那么您可以始终将其创建为一个服务。服务不会显示任何类型的UI。
如果您编写了控制台应用程序,则可以在默认情况下将其隐藏。
创建一个新的控制台应用程序,然后将"Output Type“类型更改为"Windows Application”(在项目属性中完成)
如果您使用的是Process类,则可以编写
yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;
在
yourprocess.start();
和进程将隐藏之前
简单的答案是:转到控制台应用程序的属性(项目的属性).In“应用程序”选项卡,只需将“输出类型”更改为"Windows应用程序“。就这样。
尽管这里的其他答案说您可以将“输出类型”更改为"Windows应用程序“,但请注意,这将意味着您不能使用
Console.In
,因为它将成为NullStreamReader。
然而,
Console.Out
和
Console.Error
似乎仍然工作得很好。
如果您对输出感兴趣,可以使用此函数:
private static string ExecCommand(string filename, string arguments)
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(filename);
psi.Arguments = arguments;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
process.StartInfo = psi;
StringBuilder output = new StringBuilder();
process.OutputDataReceived += (sender, e) => { output.AppendLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { output.AppendLine(e.Data); };
// run the process
process.Start();
// start reading output to events
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// wait for process to exit
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("Command " + psi.FileName + " returned exit code " + process.ExitCode);
return output.ToString();
}
它运行给定的命令行程序,等待它完成,然后以字符串的形式返回输出。
将以下代码添加到您的类中以导入DLL文件:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
const int SW_HIDE = 0;
const int SW_SHOW = 5;
然后,如果您想隐藏它,请使用以下命令:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
如果您想要显示控制台:
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
只需写
ProcessStartInfo psi= new ProcessStartInfo("cmd.exe");
......
psi.CreateNoWindow = true;
基于Adam Markowitz上面的回答,以下内容对我有效:
process = new Process();
process.StartInfo = new ProcessStartInfo("cmd.exe", "/k \"" + CmdFilePath + "\"");
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//process.StartInfo.UseShellExecute = false;
//process.StartInfo.CreateNoWindow = true;
process.Start();
我有一个通用的解决方案来分享:
using System;
using System.Runtime.InteropServices;
namespace WhateverNamepaceYouAreUsing
class Magician
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int HIDE = 0;
const int SHOW = 5;
![]() |
眼睛小的乌冬面 · vue 页面跳转之参数传递与接收参数 - 掘金 1 年前 |