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

来源:互联网 发布:人工智能技术排名第一 编辑:程序博客网 时间:2024/06/05 00:24

在命令行中给定的参数就是命令行参数。

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

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

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 就可以运行


原创粉丝点击