0

Can anyone help me output a message to a console box when my .exe is called with the wrong parameters?

Yesterday, some very kind people helped me work out how to call my app without a UI

Here is the thread

command line to make winforms run without UI

So, I have told my app to respond to "/silent archive=true transcode=true" and runs without a UI. Great!

Is it possible to output a message to the command window if they get the command incorrect?

as in "Parameters must be specified like this: /silent archive=true transcode=true"

I have tried this but nothing displays in the dos window..

static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            if (args[0] == "/silent")
            {
                bool archive = false;
                bool transcode = false;

                try
                {
                    if (args[1] == "transcode=true") { transcode = true; };
                    if (args[2] == "archive=true") { archive = true; };
                    Citrix_API_Tool.Engine.DownloadFiles(transcode, archive);
                }
                catch
                {
                    Console.Write ("Hello");
                    Console.ReadLine();
                    return;
                }
            }
        }
        else
Community
  • 1
  • 1
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89

1 Answers1

0
internal sealed class Program
{
  [DllImport("kernel32.dll")]
  private static extern bool AttachConsole(int dwProcessId);

  private const int ATTACH_PARENT_PROCESS = -1;
  [STAThread]
  private static void Main(string[] args)
  {
    if(false)//This would be the run-silent check.
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new MainForm());
    }
    try
    {
      throw new Exception("Always throw, as this tests exception handling.");
    }
    catch(Exception e)
    {
      if(AttachConsole(ATTACH_PARENT_PROCESS))
      {
        //Note, we write to Console.Error, not Console.Out
        //Partly because that's what error is for.
        //Mainly so if our output were being redirected into a file,
        //We'd write to console instead of there.
        //Likewise, if error is redirected to some logger or something
        //That's where we should write.
        Console.Error.WriteLine();//Write blank line because of issue described below
        Console.Error.WriteLine(e.Message);
        Console.Error.WriteLine();//Write blank line because of issue described below
      }
      else
      {
        //Failed to attach - not opened from console, or console closed.
        //do something else.
      }
    }
  }      
}

A problem is that the console would have already returned to taking input from the user. Hence you really want to try to have your exception as fast as you can if you're going to have it, so a fast validation check rather than an exception that might happen down the line, is preferable.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251