Mister_Spock
asked on
C#: CreateNoWindow not working
I've tried CreateNoWindow = true, UseShellExecute = false and WindowStyle = System.Diagnostics.Process WindowStyl e.Hidden but I can't get the DOS window to hide. Any suggestions?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HNTB_Defrag
{
class Program
{
public static class CommonArea
{
public const string ADMIN_NAME = "HNeedles";
public const string ADMIN_DOMAIN = "";
public const string ADMIN_PASSWORD_CURRRENT = "Sn0wFall!";
public const string ADMIN_PASSWORD_PREVIOUS = "R3dR0b1n!";
public const string COMMAND_ARGUMENTS = "c: -v -f";
public const string COMMAND_NAME = "Defrag.exe";
}//CommanArea
static void Main(string[] args)
{
//Information about the running process
System.Diagnostics.ProcessStartInfo l_obj_ProcessStart
= new System.Diagnostics.ProcessStartInfo();
l_obj_ProcessStart.Arguments = CommonArea.COMMAND_ARGUMENTS;
l_obj_ProcessStart.CreateNoWindow = true;
l_obj_ProcessStart.Domain = CommonArea.ADMIN_DOMAIN;
l_obj_ProcessStart.ErrorDialog = false;
l_obj_ProcessStart.UseShellExecute = false;
l_obj_ProcessStart.FileName = System.Environment.SystemDirectory
+ "\\" + CommonArea.COMMAND_NAME;
l_obj_ProcessStart.LoadUserProfile = false;
System.Security.SecureString l_objConvertString
= new System.Security.SecureString();
foreach (char C in CommonArea.ADMIN_PASSWORD_CURRRENT)
{
l_objConvertString.AppendChar(C);
}
l_obj_ProcessStart.Password = l_objConvertString;
l_obj_ProcessStart.UserName = CommonArea.ADMIN_NAME;
l_obj_ProcessStart.UseShellExecute = false;
l_obj_ProcessStart.WindowStyle
= System.Diagnostics.ProcessWindowStyle.Hidden;
l_obj_ProcessStart.WorkingDirectory = System.Environment.SystemDirectory;
System.Diagnostics.Process l_objProcess
= new System.Diagnostics.Process();
l_objProcess.StartInfo = l_obj_ProcessStart;
l_objProcess.Start();
}//Main
}
}
ASKER
No, the user base here will freak and try to shut it down.
This is what I've gathered from 5 minutes of testing...
Using CreateNoWindow causes the child process to be started with an invisible console attached to it. If the child process subsequently detaches from that console, and attaches to a new console window, then a you will see a console window regardless of the state of the original console. i.e., regardless of whether CreateNoWindow is specified or not, the code below will always show a console window. You may not be able to control this for defrag.exe in particular, maybe look into a 3rd party defrag tool - I've taken a liking to dirms (Google around for it).
Using CreateNoWindow causes the child process to be started with an invisible console attached to it. If the child process subsequently detaches from that console, and attaches to a new console window, then a you will see a console window regardless of the state of the original console. i.e., regardless of whether CreateNoWindow is specified or not, the code below will always show a console window. You may not be able to control this for defrag.exe in particular, maybe look into a 3rd party defrag tool - I've taken a liking to dirms (Google around for it).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleTest
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();
[DllImport("kernel32")]
static extern bool AllocConsole();
static void Main(string[] args)
{
ulong l = 0;
FreeConsole(); // Detach from "invisible" console
bool success = AllocConsole(); // allocate a new, visible, console
Console.WriteLine("AllocConsole() succeeded: " + success.ToString());
while (true)
{
l += 1;
Console.WriteLine(l.ToString());
System.Threading.Thread.Sleep(250);
}
}
}
}
The on-line help for the CreateNoWindow property states:
If the UserName and Password properties are not nullNothingnullptra null reference (Nothing in Visual Basic), the CreateNoWindow property value is ignored and a new window is created.
I see you're setting the UserName and Password properties, so that would explain why CreateNoWindow is not having an effect.
I'm wondering if the same thing happens with the WindowStyle property. The help for WindowStyle doesn't state explicitly one way or the other, but just for grins you might try testing it not setting the UserName and Password fields. That would tell you for sure whether that is the cause of the WindowStyle = ProcessWindowStyle.Hidden being ignored.
If the UserName and Password properties are not nullNothingnullptra null reference (Nothing in Visual Basic), the CreateNoWindow property value is ignored and a new window is created.
I see you're setting the UserName and Password properties, so that would explain why CreateNoWindow is not having an effect.
I'm wondering if the same thing happens with the WindowStyle property. The help for WindowStyle doesn't state explicitly one way or the other, but just for grins you might try testing it not setting the UserName and Password fields. That would tell you for sure whether that is the cause of the WindowStyle = ProcessWindowStyle.Hidden being ignored.
ASKER
Is there a way to minimixe the window? I have tried this as well with no luck.
even with the username and pass not set you will still see the 'flash' of dos command
if you minimize it will still be there. won't your userbase 'freakout' about that?
ASKER
No, if it if out of sight it is OK. I would rather hide the window but minimize would be a valid work around.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what if you made this a form project instead of a dos?
doh!
are you wanting it not to even pop up for a split second?