C# 将 args(参数)传递给窗体应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:27:20  来源:igfitidea点击:

passing args (arguments) into a window form application

c#windowsarguments

提问by balexandre

I have my Windows Application that accepts args and I use this in order to set up the Window behaviour

我有接受 args 的 Windows 应用程序,我使用它来设置窗口行为

problem is that I need to pass text in some of this arguments but my application is looking at it as multiple args, so, this:

问题是我需要在其中一些参数中传递文本,但我的应用程序将其视为多个参数,因此:

"http://www.google.com/" contact 450 300 false "Contact Info" true "Stay Visible" true

has actually 11arguments instead of the 9that I am expecting.

实际上有11 个参数,而不是我期望的9 个

What is the trick to get "contact info"and "stay visible"to be passed as only one argument?

“联系信息”“保持可见”仅作为一个参数传递的技巧是什么?

采纳答案by Jon Skeet

Are you running it directly from the command line? If so, I'd expect that to work just fine. (I assume you're using the parameters from the Main method, by the way?)

你是直接从命令行运行它吗?如果是这样,我希望它能正常工作。(顺便说一下,我假设您正在使用 Main 方法中的参数?)

For instance, here's a small test app:

例如,这是一个小型测试应用程序:

using System;

class Test
{
    static void Main(string[] args)
    {
        foreach (string arg in args)
        {
            Console.WriteLine(arg);
        }
    }
}

Execution:

执行:

>test.exe first "second arg" third
first
second arg
third

This is a console app, but there's no difference between that and WinForms in terms of what gets passed to the Main method.

这是一个控制台应用程序,但就传递给 Main 方法的内容而言,它与 WinForms 没有区别。

回答by Xn0vv3r

MSDN says, that it should work the way you mentioned.

MSDN 说,它应该按照您提到的方式工作。

class CommandLine
{
    static void Main(string[] args)
    {
        // The Length property provides the number of array elements
        System.Console.WriteLine("parameter count = {0}", args.Length);

        for (int i = 0; i < args.Length; i++)
        {
            System.Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
        }
    }
}

回答by Sani Singh Huttunen

How are you executing your application?
If you execute it from another application you might have forgotten to format the argument string properly:

你如何执行你的应用程序?
如果您从另一个应用程序执行它,您可能忘记正确格式化参数字符串:

String arguments = "First \"Sec ond\" Third Fourth \"Fi fth\""

would have five arguments whereas

将有五个参数,而

String arguments = "First Sec ond Third Fourth Fi fth"

would have seven.

会有七个。

If the arguments are in the shortcut target property then the same applies:

如果参数在快捷方式目标属性中,则同样适用:

"C:\My Path\MyApplication.exe" "Argument 1" Argument2 Argument3 "Argument 4"

instead of

代替

"C:\My Path\MyApplication.exe" Argument 1 Argument2 Argument3 Argument 4