程序将无法识别命令行参数

时间:2012-09-19 19:31:25

标签: c# .net command-line-arguments

  

可能重复:
  How to get command line from a ClickOnce application?

我正在开发一个控制台应用程序,并在我已完成其他一些工作之后手动在Main()中添加了string[] args。这就是我要接受命令行参数的全部内容吗?或者我还需要在其他地方配置一些东西吗?无论我在exe之后发送什么,我都会继续Console.WriteLine("{0}",args.Length)并获得零。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", args.Length);
    }
}

然后我运行...\setup.exe yes no maybe并获得0长度。我还需要做什么?

更多信息:

我在属性页面中设置命令行参数后试图中断,我收到以下错误。:

enter image description here

我认为有人对ClickOnce部署的评论是我的问题。如何在VS2010中部署以实现此目的?

更多信息:

我在“属性”下打开了“ClickOnce安全设置” - >安全性并且能够成功调试,但是当我单击“发布”时,它会自动重新启用此设置..如何防止这种情况?

2 个答案:

答案 0 :(得分:3)

这个例子:

using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Number of command line parameters = {0}", args.Length);
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
            }
        }
    }
}

执行时ConsoleApplication1.exe a b c将输出:

Number of command line parameters = 3
Arg[0] = [a]
Arg[1] = [b]
Arg[2] = [c]

请参阅Command Line Parameters Tutorial

更新:此代码

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", args.Length);
    }
}

ConsoleApplication1.exe a b c输出

执行时
3

最重要的是,确保您正在执行正确的.exe。

答案 1 :(得分:0)

也许你可以在for循环中做这样的事情来检查任何命令参数的值是什么

public class CountCommandLineArgs
{
   public static void Main(string[] args)
   {
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       foreach(string s in args)
       {
          Console.WriteLine(s);
       }
   }
}
相关问题