Windows窗体应用程序的命令行参数 [英] Windows form application command line arguments

查看:411
本文介绍了Windows窗体应用程序的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过谷歌搜索,并在这里找到很多例子,但我似乎无法让我的Windows窗体应用程序来运行,并采取论证形成的命令行。我真的希望有调度应用程序,而无需一个控制台版本的选项。但每次我安装的CMD行参数就出现了错误有CLR20r3错误。

 静态无效的主要(字串[] args) 
{
如果(参数= NULL&放大器;!&安培; args.Length大于0)
{
/ *
* ARG [1] =备份位置*需要
* ARG [2] =日志文件 - 启用如果存在*可选的
* ARG [3] =调试消息 - 启用如果存在*可选的
* ARG [4] =备份类型 - 类型执行*为必填
* /

}
,否则
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
Application.Run(新Form1中());
}
}



每当我试图通过一个ARG是错误的前




MyApp.exe将C:\Backup\=> CLR20r3



解决方案

这是我在运行的一种形式的应用程序或根据命令行参数的无形的应用项目中使用的启动代码的一个例子。

 使用系统; 
使用System.Collections.Generic;使用System.Windows.Forms的
;

命名空间的构建文件
{
静态类节目
{
///<总结>
///的主入口点应用程序。
///< /总结>
[STAThread]
静态无效的主要()
{
INT ABCFile = -1;
字串[] args = Environment.GetCommandLineArgs();
如果((args.Length→1)及与放大器;(参数[1] .StartsWith(/ N)))
{
...无关细节omiited
ABCFile = 1;
}
}

如果(ABCFile大于0)
{
变种我=新的MainForm(); //实例的形式
me.NoGui(ABCFile); //调用备选入口点
Environment.Exit(0);
}
,否则
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新的MainForm());
}
}
}
}

请注意这只能是因为没有在我的替代入口点取决于事件等运行时环境Application.Run()方法,包括处理窗口消息等提供的。


I have searched through Google and here found many examples but I can't seem to get my Windows Form application to run and take arguments form the command line. I really want to have the option of scheduling the application without having a console version. But every time I setup the cmd line arguments it errors out with a CLR20r3 error.

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());
   }
}

Anytime I try to pass an arg it errors ex

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

解决方案

This is an example of the startup code I use in a project that runs as a form app or as a formless app depending on command line arguments.

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());
      }
    }
  }
}

Note this only works because nothing in my alternative entry point depends on events, etc. that are provided by the runtime environment Application.Run() method which includes handling windows messages, etc.

这篇关于Windows窗体应用程序的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆