Windows表单应用程序命令行参数

ond*_*vic 4 c# command-line-arguments winforms

我搜索了谷歌,在这里找到了很多例子,但我似乎无法运行我的Windows窗体应用程序并从命令行获取参数.我真的希望能够在没有控制台版本的情况下安排应用程序.但每次我设置cmd行参数时,它都会出现CLR20r3错误.

static void Main(string[] args)
{
   if(args != null && args.Length > 0)
   {
      /*
      * arg[1] = Backup Location *require
      * arg[2] = Log File - Enable if present *optional
      * arg[3] = Debug Messages - Enabled if present *optional
      * arg[4] = Backup Type - Type to perform *required
      */

   }
   else
   {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
     Application.Run(new Form1());
   }
}
Run Code Online (Sandbox Code Playgroud)

任何时候我试图传递一个arg它错误ex

myapp.exe"C:\ Backup \"=> CLR20r3

Gar*_*ker 5

这是我在项目中使用的启动代码的示例,该项目作为表单应用程序或作为无形式应用程序运行,具体取决于命令行参数.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BuildFile
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      int ABCFile = -1;
      string[] args = Environment.GetCommandLineArgs();
      if ((args.Length > 1) && (args[1].StartsWith("/n")))
      {
            ... unrelated details omiited
            ABCFile = 1;
        }
      }

      if (ABCFile > 0)
      {
        var me = new MainForm(); // instantiate the form
        me.NoGui(ABCFile); // call the alternate entry point
        Environment.Exit(0);
      }
      else
      {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这只能起作用,因为我的替代入口点中的任何内容都不依赖于运行时环境Application.Run()方法提供的事件等,其中包括处理窗口消息等.