如何从命令提示符处获取参数并运行应用程序 [英] how to get arguments from command prompt and to run the application

查看:249
本文介绍了如何从命令提示符处获取参数并运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我的c#windows应用程序中有一个函数,该函数的参数应从命令提示符处获取然后执行程序.

在此先感谢

解决方案

如果您使用的是控制台应用程序,则main方法应将字符串数组作为包含所有参数的参数.

例如.下面的代码显示所有命令行参数.

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}


 字符串 [] System.Environment.GetCommandLineArgs()


Main的参数是命令行参数.如果您使用的是标准Visual Studio模板,则可以在Program.cs中找到Main方法.如果我在Windows应用程序中需要它们,则通常会修改Main方法,以将参数数组传递到主窗体的构造函数中,然后在其中进行处理.

Ralphlee1的答案也是正确的,但我更希望从单个入口点将它们作为参数传递,因为那样看起来更干净,因为外部信息仅在单个位置输入.


i have a requirement such that there is a function in my c#windows application arguments to that function should get from command prompt and then execute the program .how to do that??


thanks in advance

解决方案

If you are using a console application, the main method woould have a string array as a parameter that would contain all the parameters.

E.g. The code below displays all the command line arguments.

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}


string[] System.Environment.GetCommandLineArgs()


The arguments to Main are the command line arguments. If you''re using the standard Visual Studio template, you can find the Main method in Program.cs. If I need them in a Windows application I generally modify that Main method to pass the array of arguments into the main form''s constructor, and handle them in there.

Ralphlee1''s answer is also correct but I prefer to pass them as arguments from the single entry point because that seems cleaner, as the external information is only being fed in in a single place.


这篇关于如何从命令提示符处获取参数并运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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