将命令行参数传递到控制台应用程序不起作用 [英] Passing command line arguments to console application does not work

查看:137
本文介绍了将命令行参数传递到控制台应用程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't pass arguments to my console app. I tried it like this:

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

当我运行带有参数的应用程序时,该应用程序退出运行且没有任何消息。

When I run the app with arguments, the app quits running without any messages.

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

When debugging there is nothing in string[] args.

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

My C# project is a plain .net 4.5.2 command line project. My OS is 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]);
            }
        }
    }
}

推荐答案

我进一步简化了代码;即,我们现在将查看

I simplified your code a bit further; namely we will now look at

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.
    }
}

,以便我们以两种方式获得输出。

so that we get an output either way.

不加任何花费就可以运行

Running this without any further ado will yield


没有传递参数。

No arguments were passed.

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

However, in Visual Studio, by going into


项目->属性->调试

Project -> Properties -> Debug

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

we will now provide some command argument lines for debugging.

立即运行程序将产生


计数:3 First:-first

Count: 3 First: -first






要在现实世界中使用,您可以像下面这样运行该应用程序(例如,从命令行):


For real world use, you can then run the app (e.g. from the command line) like this:

app.exe -one /two three foo

并且仍将获取所有命令行参数:

and it will still get all of the command line arguments:


计数:4首先:-one

Count: 4 First: -one

这篇关于将命令行参数传递到控制台应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆