用c#写的Windows应用程序怎样实现命令行参数运行?

来源:百度知道 编辑:UC知道 时间:2024/05/31 04:06:14
比如说运行MyApp.exe -h可以让程序启动后最小化。
注意:我说的是Windows应用程序,不是控制台应用程序。在前者的Program.cs中有个Main方法,但是无参数,是否可以直接添加参数,然后传给其内部调用的程序Form构造函数,再直接在后者定义中添加参数?还是应通过编译器来执行添加?

应该是修改应用程序的主入口点的吧

[STAThread]
static void Main() // {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

本示例演示如何输出命令行参数。
// cmdline1.cs
// arguments: A B C
using System;

public class CommandLine
{
public static void Main(string[] args)
{
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
for(int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}