您的位置:首页 > 编程语言 > C#

c#、winfrom 给程序添加命令行参数

2013-11-29 15:47 417 查看
在命令行中给定的参数就是命令行参数。

命令行的参数以空格隔开。

但是,若命令行的参数本身包含空格时,则该参数必须用一对双引号括起来。

eg:d:\loading.exe -t=5000

//Program.cs

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new WelCome(args));
}
}


//程序获取

/// <summary>
/// 配置
/// </summary>
public WelCome(string[] args)
{
InitializeComponent();
foreach (string arg in args)
{
//传递的参数-t为窗体显示时间
if (arg.Contains("-t="))
{
string strTime = arg.Substring(arg.IndexOf('=') + 1);
int time = 200;
if (int.TryParse(strTime, out time))
{
this.kltimer.Interval = time;
}
}
}
}


总结:

使用可以传一个string数组的main程序入口,数组里的字符就是命令行参数

windows+r 输入 exepath args1 args2 就可以运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# winfrom 命令行参数