【问题标题】:passing args (arguments) into a window form application将 args(参数)传递到窗口窗体应用程序
【发布时间】:2010-10-02 15:59:13
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: c# windows arguments


    【解决方案1】:

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

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

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

    执行:

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

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

    【讨论】:

    • 约翰这是在作弊...首先进入 :-) 但第一次只给出了一半的答案:-)
    • @Michael:我有这个程序,但它在另一台计算机上。我必须回答+编辑才能把它全部放在那里:)
    • 我实际上是在传递 \"Stay connect\" :( - 我在构建参数中使用了复制/粘贴...也许这就是 iPhone 没有复制(粘贴功能;)谢谢乔恩.
    【解决方案2】:

    MSDN says,它应该按照你提到的方式工作。

    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]);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

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

      会有五个参数,而

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

      会有七个。

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

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

      而不是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2015-05-08
        • 1970-01-01
        相关资源
        最近更新 更多