C# Windows窗体应用程序命令行参数

C# Windows窗体应用程序命令行参数,c#,winforms,command-line-arguments,C#,Winforms,Command Line Arguments,我在Google上搜索了很多例子,但我似乎无法让我的Windows窗体应用程序运行并从命令行获取参数。我真的希望能够选择在没有控制台版本的情况下调度应用程序。但每次我设置cmd行参数时,它都会出现一个CLR20r3错误 static void Main(string[] args) { if(args != null && args.Length > 0) { /* * arg[1] = Backup Location *require

我在Google上搜索了很多例子,但我似乎无法让我的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());
   }
}
每当我试图传递参数时,它都会出错

myapp.exe“C:\Backup\”=>CLR20r3


这是我在项目中使用的启动代码的一个示例,该项目根据命令行参数以表单应用程序或无表单应用程序的形式运行

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());
      }
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间构建文件
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
int ABCFile=-1;
字符串[]args=Environment.GetCommandLineArgs();
如果((args.Length>1)和(&(args[1].StartsWith(“/n”))
{
…不相关的细节被删除
ABCFile=1;
}
}
如果(ABC文件>0)
{
var me=new MainForm();//实例化表单
me.NoGui(ABCFile);//调用备用入口点
环境。退出(0);
}
其他的
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的MainForm());
}
}
}
}

注意:这只适用于我的备用入口点,因为它不依赖于运行时环境应用程序提供的事件等。Run()方法包括处理windows消息等。

这真的是您的全部代码吗?根据你发布的内容,我看不到任何地方可以传递论点。你收到了什么例外?您可能可以在事件查看器中找到它。@Brian不,不是我所有的代码,只是它的开头,以确保我做得正确。您试过调试该应用程序吗?您可以告诉IDE传递命令行参数。您还可以在尝试解析之前将args的内容转储到日志文件中,以确认应用程序正在接收您认为应该接收的内容。
System.NullReferenceException
由于主args不适用于Windows窗体应用程序,您应该使用
string[]args=Environment.GetCommandLineArgs()