Reputation:
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
Upvotes: 1
Views: 20932
Reputation: 3371
You could create a WinForms app and hide the MainForm by Minimizing it and not showing it in the taskbar on initialization.
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.
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...
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();
}
}
Upvotes: 0
Reputation: 2857
Set WindowStyle to 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();
Upvotes: 2