I currently have an .exe I am running in my C# code, the .exe collects a list of files from specific locations, once it runs it prompts user to press esc to exit or enter to re-collect. Because I am trying to run this as an automated process from my code I would like to add a command line argument like -s or something to bypass the prompt for user input. without bypassing this the .exe will never close and the software will eventually crash.
Here is my .exe code
class StackGrabber
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
StackReader.MessageHandler.OnMessage += new StackReader.MessageHandler.NotificationHandler(OnNotification);
StackGrabber grabber = new StackGrabber();
grabber.Grab();
}
catch (Exception ex)
{
DisplayMessage(ex.ToString());
}
}
private volatile bool IsComplete = false;
private StackReader.StackReader reader;
void Grab()
{
try
{
DisplayMessage("Grabbing stacks... \n");
reader = new StackReader.StackReader();
reader.OnReadComplete += new System.Windows.Forms.MethodInvoker(OnComplete);
if (reader.ReadStacks() > 0)
{
while (!reader.IsComplete)
Thread.Sleep(500);
}
else
{
Thread.Sleep(500);
}
DisplayMessage(Environment.NewLine + "Press esc to exit" + Environment.NewLine);
//...
//...
Console.Read();
}
catch (Exception ex)
{
DisplayMessage(ex.ToString());
}
}
public static void OnNotification(string msg)
{
DisplayMessage(msg);
}
public void OnComplete()
{
IsComplete = true;
}
private static object ConsoleLock = new object();
private static void DisplayMessage(string msg)
{
lock (ConsoleLock)
{
Console.WriteLine(msg);
}
}
}
and I call the executable like this:
StackGrab.StartInfo.FileName = @"..\\bin\\StackGrabber.exe";
StackGrab.StartInfo.CreateNoWindow = true;
StackGrab.StartInfo.UseShellExecute = false;
StackGrab.Start();
StackGrab.WaitForExit();
I want to be able to call StackGrabber.exe in a command prompt so that I put C:\>StackGrabber -s and it doesnt wait for ‘esc’ input to exit
You could add a boolean flag to your StackGrabber class to indicate if the user should not be prompted. You’ll need to set it to true at initialization. Then set use Array.Exists on the args to find your parameter.
Then check it later in the grab function:
Edit:The args parameter of of the Main function is the command line parameters that were supplied to the program. Here is an article on how to use command line arguments.