C#在开始时隐藏程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1209635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# hide program at start
提问by
I'm looking for some way to run my program with no windows, I read this :
我正在寻找某种方法来在没有窗口的情况下运行我的程序,我读到了这个:
string exePath = string.Format("{0}\{1}", dir, "theUtility.exe");
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo( exePath );
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = dir;
System.Diagnostics.Process process = System.Diagnostics.Process.Start( startInfo );
// Wait for it to die...
process.WaitForExit();
But I'm looking for another way , this code show windows and run another file , but I need start program without any windows.
但我正在寻找另一种方式,此代码显示窗口并运行另一个文件,但我需要在没有任何窗口的情况下启动程序。
thanks
谢谢
回答by Mez
Set WindowStyle to ProcessWindowStyle.Hidden;
将 WindowStyle 设置为 ProcessWindowStyle.Hidden;
example
例子
Process proc = new Process();
proc.StartInfo.FileName = Path.Combine("cmd.exe","");
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
回答by Wil P
You could create a WinForms app and hide the MainForm by Minimizing it and not showing it in the taskbar on initialization.
您可以创建一个 WinForms 应用程序并通过最小化它来隐藏 MainForm,而不是在初始化时在任务栏中显示它。
Form.WindowState = FormWindowState.Minimized;
Form.ShowInTaskBar = false;
Then you could run whatever code you wanted within the message loop for MainForm w/out any UI. If at some point you wanted a user interface you could show main form or any other number of forms.
然后,您可以在 MainForm 的消息循环中运行您想要的任何代码,而无需任何 UI。如果在某个时候你想要一个用户界面,你可以显示主表单或任何其他数量的表单。
public partial class Form1 : Form
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MessageBox.Show("Loaded");
}
}
Or, you could possibily just launch your console program as a hidden window and use a pinvoke as described in the following post...
或者,您可以将控制台程序作为隐藏窗口启动并使用 pinvoke,如下面的帖子中所述...
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ffe733a5-6c30-4381-a41f-11fd4eff9133/
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ffe733a5-6c30-4381-a41f-11fd4eff9133/
class Program
{
const int Hide = 0;
const int Show = 1;
[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
static void Main(string[] args)
{
Console.WriteLine("Press any key to hide me.");
Console.ReadKey();
IntPtr hWndConsole = GetConsoleWindow();
if (hWndConsole != IntPtr.Zero)
{
ShowWindow(hWndConsole, Hide);
System.Threading.Thread.Sleep(5000);
ShowWindow(hWndConsole, Show);
}
Console.ReadKey();
}
}