【问题标题】:Passing command line arguments to console application does not work将命令行参数传递给控制台应用程序不起作用
【发布时间】:2018-06-08 17:52:40
【问题描述】:

我无法将参数传递给我的控制台应用程序。 我试过这样:

App.exe arg1
App.exe "arg1"
App.exe

当我使用参数运行应用程序时,应用程序会退出运行而没有任何消息。

调试时string[] args 中没有任何内容。

我的 C# 项目是一个普通的 .net 4.5.2 命令行项目。 我的操作系统是 Windows 10 x64。

示例代码:

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace Setup
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                using (PowerShell psInstance = PowerShell.Create())
                {
                    string cmd = "Set-ExecutionPolicy RemoteSigned -Force; " +
                                 "echo \"Set-ExecutionPolicy RemoteSigned -Force\"; echo \"\"; " +
                                 "Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force; ";

                    psInstance.AddScript(cmd);

                    Collection<PSObject> PSOutput = psInstance.Invoke();

                    Console.WriteLine(psInstance.Streams.Error[0].ErrorDetails.Message);

                    foreach (var item in PSOutput)
                    {
                        if (item != null)
                        {
                            Console.WriteLine(item.BaseObject.ToString());
                        }
                    }
                    Console.ReadLine();
                }
            }
            else
            {
                // Will not work
                Console.WriteLine(args[0]);
            }
        }
    }
}

有人有想法吗?

【问题讨论】:

  • 请包含您为 App.exe 编写的代码。这可能很简单。
  • 我已经编辑了我的问题。
  • 您确定要向您的应用传递参数吗?假设您从 Visual Studio 运行此程序,项目属性中 Debug 选项卡上的文本框 Command line arguments 中是否有任何内容?
  • @Marcus 请看看我的回答是否对你有帮助。如果没有,我很乐意帮助您解决实际问题。

标签: c# console-application command-line-arguments


【解决方案1】:

我进一步简化了您的代码;即我们现在来看看

public class Program
{
    public static void Main(string[] args)
    {
        int numberOfArguments = args.Length;

        if (numberOfArguments > 0)
        {
            Console.WriteLine($"Count: {numberOfArguments} First: {args[0]}");
        }
        else
        {
            Console.WriteLine("No arguments were passed.");
        }

        Console.ReadLine(); // Keep the console open.
    }
}

这样我们就可以得到输出。


毫不费力地运行它会产生

没有传递任何参数。

但是,在 Visual Studio 中,通过进入

项目 -> 属性 -> 调试

我们现在将提供一些用于调试的命令参数行。

现在运行程序将产生

计数:3 第一:-first


对于实际使用,您可以像这样运行应用程序(例如从命令行):

app.exe -one /two three foo

它仍然会得到所有的命令行参数:

计数:4 第一个:-一个

【讨论】:

    【解决方案2】:

    如果您可以分享代码,将会很有帮助。

    打印零位参数的示例程序将是:

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(args[0]);
            }
        }
    }
    

    你可以从命令行调用它

    ConsoleApplication1.exe argument1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多