在命令提示符下设置命令行参数 [英] Set Command line arguments on command prompt

查看:109
本文介绍了在命令提示符下设置命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过命令提示符设置命令行参数?

Is it possible to set command line arguments through command prompt?

我正在尝试创建同一项目的不同实例,这些实例接受不同的命令行参数。

I am trying to create different instances of the same project that takes in different command line arguments.

推荐答案

Hi Mirai_away,

Hi Mirai_away,

欢迎来到MSDN论坛。

Welcome to MSDN forum.

>> 是否可以设置通过命令提示符命令行参数?

>>Is it possible to set command line arguments through command prompt?

是的,这是可能的。但是项目的输出应该是.exe文件。

Yes, it's possible. But the output of the project should be a .exe file.

我们可以按照以下步骤轻松检查命令行参数的工作原理:

And we can easily check how the command-line arguments works following steps below:

1.创建一个新的C#控制台项目(.net框架)

1.Create a new C# console project(.net framework)

2.添加下面的代码脚本:

2.Add code script below:

static void Main(string[] args)
        {
            //let's set the program to receive one parameter
            if (args.Length!=1)
            {
                Console.WriteLine("TestCommand.exe only receives one argument and must need one!");
            }
            //choose which instance according the argument
            switch (args[0])
            {
                case "Test":
                    Console.WriteLine("This is Test mode.");
                    // Add code here to do sth about Test mode
                    break;
                case "Develop":
                    Console.WriteLine("This is Develop mode.");
                    // Add code here to do sth about Develop mode
                    break;
                    default:
                        Console.WriteLine("The argument is not recognized.");
                        break;
            }
        }

3.现在我们可以通过设置命令行参数来创建不同的实例,请参阅:

3.Now we can create different instance by setting command-line arguments, see:

此外:

在解决方案探索中,您可以右键单击Project => Properties => Debug =>命令行参数以在VS中设置命令行参数。 (当您开发和调试项目时)

In Solution explore, you can Right-click Project=>Properties=>Debug=>Command-line arguments to set command-line arguments in VS. (When you're developing and debugging the project)

这篇关于在命令提示符下设置命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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