如何使用命令行参数启动我的程序,选择不同的设置?

How to start my program, with different settings selected, using a command line parameter?

在我的 windows 表单应用程序的 "settings" 选项卡中,我有一个复选框,如果选中,它将禁用我的程序的某些功能,以使其更兼容。默认情况下,该复选框未选中(启用所有功能)。

我想要实现的是让用户可以选择通过带有“/c”参数的 cmd 启动我的程序(例如:myprogram.exe /c)这将使我的程序在未选中该复选框的情况下启动(禁用某些功能)

通过这种方式,在我的主窗体加载期间,我可以得到类似的东西:

if (program launched with /c parameter)
{
    checkbox1.checked = false; // or something else 
}

在此先感谢您的帮助。

我相信你正在寻找

Environment.GetCommandLineArgs();

这将允许您访问您的命令行参数,您可以关闭它来切换您的开关。

如果您使用的是 Linq,则可以执行类似这样的操作来获取您要查找的布尔值:

var args = Environment.GetCommandLineArgs();
if(args.Any(arg => arg == "/c"))
{
    checkbox1.checked = false;
}

可以找到其他文档 here

使用 Environment.GetCommandLineArgs(),这将 return 参数的字符串数组,第一个元素始终是应用程序路径。

string[] array = Environment.GetCommandLineArgs();
if (array.Length > 1 && array[1] == "/c")
    //Do something

您还可以在 main 方法中使用 args 数组。

public static void Main(string[] args)
{
    if (array.Length > 0 && array[0] == "/c")
        //Do something
}
if (System.Environment.CommandLine.Contains("/c")){
    CheckBox1.Checked = False;
}