我有两个不同的WinForms应用程序,AppA和AppB。两者都运行.NET 2.0。
在AppA中,我想打开AppB,但是我需要将命令行参数传递给它。如何使用在命令行中传递的参数?
这是我当前在AppB中使用的主要方法,但我认为您不能更改此方法?
  static void main()
  {
  }
          我有两个不同的WinForms应用程序,AppA和AppB。两者都运行.NET 2.0。
在AppA中,我想打开AppB,但是我需要将命令行参数传递给它。如何使用在命令行中传递的参数?
这是我当前在AppB中使用的主要方法,但我认为您不能更改此方法?
  static void main()
  {
  }
          Answers:
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
              为Winforms应用程序使用args的最佳方法是使用
string[] args = Environment.GetCommandLineArgs();
您可能可以将其与枚举结合使用,以巩固整个代码库中数组的使用。
“而且您可以在应用程序中的任何地方使用它,而不仅限于像控制台应用程序那样在main()方法中使用它。”
发现于:HERE
main(string[] args)吗?
                    您可以通过访问Environment.CommandLine属性来获取任何.Net应用程序的命令行。它会将命令行作为单个字符串显示,但是解析出您要查找的数据并不难。
具有空的Main方法不会影响此属性或其他程序添加命令行参数的能力。
考虑您需要开发一个程序,通过该程序您需要传递两个参数。首先,您需要打开Program.cs类,并在Main方法中添加参数,如下所示,并将这些参数传递给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窗体类中,添加一个参数化的构造函数,该构造函数接受Program类的输入值,如下所示。
public Form1(string s, int i)
{
    if (s != null && i > 0)
       MessageBox.Show(s + " " + i);
}
要对此进行测试,您可以打开命令提示符并转到放置该exe的位置。输入文件名,然后指定parmeter1参数2。例如,见下文
C:\MyApplication>Yourexename p10 5
在上面的C#代码中,它将提示一个带有value的消息框p10 5。
您使用以下签名:(在c#中)static void Main(string [] args)
本文还可以帮助解释主要功能在编程中的作用:http : //en.wikipedia.org/wiki/Main_function_(编程 )
这是给您的一个小例子:
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");
    }
}
              这可能不是每个人都流行的解决方案,但是即使使用C#,我也喜欢Visual Basic中的应用程序框架。
添加参考 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;