gpt4 book ai didi

c# - 使用 Windows 形式的命令行参数

转载 作者:行者123 更新时间:2023-12-04 16:31:23 26 4
gpt4 key购买 nike

我有 C# Windows 窗体应用程序,我正在做很多复选框并将它们写入和读取到名为 test.json 的 JSON 格式文件中。 .现在我想将我的 test.json 与 program.exe 一起使用,这样我的 program.exe 就会像 test.json 中所写的那样选中复选框。所以我创建了 Load event handler并想使用我的 GetCommandLineArgs() .

private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
}

我知道我有 2 个参数。 [0] - program.exe[1] - test.json .需要一些想法如何使它工作。

我的问题是:

如何制作 2 个元素的 args 列表。其中 0 是 program.exe,1 是 test.json。以及当可能没有参数或只有一个参数时如何使用 args.length

最佳答案

How to make that args list of 2 elements. Where 0 is program.exe and 1 is test.json



为此,您可以通过打开项目属性窗口并导航到“调试”选项卡来从 Visual Studio 中进行调试,从那里您可以指定所需的命令行参数,如下所示:

Entering command line arguments for debugging

您可以使用 Environment.GetCommandLineArgs() ,在我看来有点奇怪。它为您提供当前正在运行的可执行文件名称。所以在你的情况下,你最终会得到你所要求的。
[0] - program.exe and [1] - test.json

How to work with args.Length when there is possibility that there will be no parameters or only one parameter.



这个很简单, Environment.GetCommandLineArgs()返回 string[] .检查所需的长度并相应地处理它。
var commandLineArgs = Environment.GetCommandLineArgs();
if (commandLineArgs.Length > 0)
{
// Do something magical...
}
else
{
// Nothing was passed in...
}

关于c# - 使用 Windows 形式的命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com