简体   繁体   English

如果命令行参数不正确,则将消息输出到控制台窗口

[英]Output message to Console window if command line parameters incorrect

Can anyone help me output a message to a console box when my .exe is called with the wrong parameters? 用错误的参数调用.exe时,有人可以帮助我将消息输出到控制台吗?

Yesterday, some very kind people helped me work out how to call my app without a UI 昨天,一些非常友善的人帮助我确定了如何在没有UI的情况下调用我的应用

Here is the thread 这是线程

command line to make winforms run without UI 使Winforms在没有UI的情况下运行的命令行

So, I have told my app to respond to "/silent archive=true transcode=true" and runs without a UI. 因此,我告诉我的应用程序响应“ / silent archive = true转码= true”,并且在没有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" 如“必须这样指定参数:/ silent archive = true转码= true”

I have tried this but nothing displays in the dos window.. 我已经尝试过了,但是在dos窗口中什么也没显示。

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
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. 因此,如果您要拥有异常,那么您真的想尝试尽可能快地处理异常,因此最好使用快速验证检查,而不是可能发生的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM