代码之家  ›  专栏  ›  技术社区  ›  MRFerocius

如何将命令行参数传递给WinForms应用程序?

  •  81
  • MRFerocius  · 技术社区  · 17 年前

    我有两个不同的WinForms应用程序,AppA&AppB。两者都在运行.NET 2.0。

    在appa中,我想打开appb,但我需要向它传递命令行参数。如何使用我在命令行中传递的参数?

    这是我目前在AppB中的主要方法,但我认为你不能改变它?

      static void main()
      {
      }
    
    6 回复  |  直到 10 年前
        1
  •  101
  •   Thomas    10 年前
    static void Main(string[] args)
    {
      // For the sake of this example, we're just printing the arguments to the console.
      for (int i = 0; i < args.Length; i++) {
        Console.WriteLine("args[{0}] == {1}", i, args[i]);
      }
    }
    

    参数将存储在 args 字符串数组:

    $ AppB.exe firstArg secondArg thirdArg
    args[0] == firstArg
    args[1] == secondArg
    args[2] == thirdArg
    
        2
  •  157
  •   Tushar Gupta - curioustushar    13 年前

    为WinForms应用程序使用args的最佳方法是使用

    string[] args = Environment.GetCommandLineArgs();
    

    你可以把这个和 枚举 为了巩固对数组的使用,需要删除代码库。

    “您可以在应用程序的任何地方使用它,而不仅仅是 仅限于在main()方法中使用,如在控制台中使用 申请。”

    发现于: HERE

        3
  •  12
  •   JaredPar    17 年前

    通过访问environment.command line属性,可以获取任何.NET应用程序的命令行。它将命令行作为一个字符串,但是解析出您要查找的数据并不太困难。

    主方法为空不会影响此属性或其他程序添加命令行参数的能力。

        4
  •  8
  •   Sarath Subramanian    10 年前

    考虑到您需要开发一个程序,通过它您需要传递两个参数。首先,你需要打开 程序CS 类并在中添加参数 主要的 方法,如下所示,并将这些参数传递给Windows窗体的构造函数。

    static class Program
    {    
       [STAThread]
       static void Main(string[] args)
       {            
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1(args[0], Convert.ToInt32(args[1])));           
       }
    }
    

    在Windows窗体类中,添加一个接受输入值的参数化构造函数。 程序 分类如下。

    public Form1(string s, int i)
    {
        if (s != null && i > 0)
           MessageBox.Show(s + " " + i);
    }
    

    要测试这一点,您可以打开命令提示符并转到该exe所在的位置。给出文件名,然后参数1参数2。例如,请参见下面的

    C:\MyApplication>Yourexename p10 5
    

    从上面的c代码中,它将提示一个带有值的消息框 p10 5 .

        5
  •  7
  •   KreepN    14 年前

    您使用此签名:(在c)静态void main(string[]args)

    本文还可以帮助解释主要函数在编程中的作用: http://en.wikipedia.org/wiki/Main_function_(programming)

    下面是一个小例子:

    class Program
    {
        static void Main(string[] args)
        {
            bool doSomething = false;
    
            if (args.Length > 0 && args[0].Equals("doSomething"))
                doSomething = true;
    
            if (doSomething) Console.WriteLine("Commandline parameter called");
        }
    }
    
        6
  •  4
  •   Jürgen Steinblock    12 年前

    这可能不是每个人都喜欢的解决方案,但我喜欢Visual Basic中的应用程序框架,即使在使用C_时也是如此。

    添加对的引用 Microsoft.VisualBasic

    创建一个名为WindowsFormsApplication的类

    public class WindowsFormsApplication : WindowsFormsApplicationBase
    {
    
        /// <summary>
        /// Runs the specified mainForm in this application context.
        /// </summary>
        /// <param name="mainForm">Form that is run.</param>
        public virtual void Run(Form mainForm)
        {
            // set up the main form.
            this.MainForm = mainForm;
    
            // Example code
            ((Form1)mainForm).FileName = this.CommandLineArgs[0];
    
            // then, run the the main form.
            this.Run(this.CommandLineArgs);
        }
    
        /// <summary>
        /// Runs this.MainForm in this application context. Converts the command
        /// line arguments correctly for the base this.Run method.
        /// </summary>
        /// <param name="commandLineArgs">Command line collection.</param>
        private void Run(ReadOnlyCollection<string> commandLineArgs)
        {
            // convert the Collection<string> to string[], so that it can be used
            // in the Run method.
            ArrayList list = new ArrayList(commandLineArgs);
            string[] commandLine = (string[])list.ToArray(typeof(string));
            this.Run(commandLine);
        }
    
    }
    

    将main()例程修改为如下所示

    static class Program
    {
    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            var application = new WindowsFormsApplication();
            application.Run(new Form1());
        }
    }
    

    这种方法提供了一些额外的有用功能(如SplashScreen支持和一些有用的事件)

    public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d.
    public event ShutdownEventHandler Shutdown;
    public event StartupEventHandler Startup;
    public event StartupNextInstanceEventHandler StartupNextInstance;
    public event UnhandledExceptionEventHandler UnhandledException;
    
    推荐文章