如何获取命令行参数并将其放入变量?

Mih*_*ski 5 c# variables parameters

我正在尝试提出申请。有人可以帮助我如何获取命令行参数并将其放入变量/字符串中。我需要在C#上执行此操作,并且它必须是5个参数。

第一个参数需要放入Title变量中。第二个参数需要放入Line1变量中。第三个参数需要放入Line2变量中。第四个参数需要放入Line3变量中。并且第五个参数需要放入Line4变量中。

坦克帮你!

编辑:

我需要将此添加到Windows窗体应用程序。

Jas*_*zun 9

您可以通过以下两种方式之一来进行操作。

第一种方法是使用string[] args它并将其从传递Main给您Form,如:

// Program.cs
namespace MyNamespace
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyForm(args));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后MyForm.cs执行以下操作:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm(string[] args)
        {
            if (args.Length == 5)
            {
                Title = args[0];
                Line1 = args[1];
                Line2 = args[2];
                Line3 = args[3];
                Line4 = args[4];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用Environment.GetCommandLineArgs(),如下所示:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm()
        {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length == 6)
            {
                // note that args[0] is the path of the executable
                Title = args[1];
                Line1 = args[2];
                Line2 = args[3];
                Line3 = args[4];
                Line4 = args[5];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而您只需离开Program.cs它的原始状态,而无需使用string[] args


Eri*_*rer 7

考虑使用库为您完成所有这些解析。我在 CodePlex 上使用命令行解析器库取得了成功:

http://commandline.codeplex.com/

您可以使用 Nuget 获取此库:

Install-Package CommandLineParser
Run Code Online (Sandbox Code Playgroud)

这是一个示例用法:

// Define a class to receive parsed values
class Options
{
    [Option('r', "read", Required = true, HelpText = "Input file to be processed.")]
    public string InputFile { get; set; }

    [Option('v', "verbose", DefaultValue = true, HelpText = "Prints all messages to standard output.")]
    public bool Verbose { get; set; }

    [ParserState]
    public IParserState LastParserState { get; set; }

    [HelpOption]
    public string GetUsage() {
    return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
    }
}

// Consume them
static void Main(string[] args)
{
    var options = new Options();

    if (CommandLine.Parser.Default.ParseArguments(args, options))
    {
        // Values are available here
        if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
    }
}
Run Code Online (Sandbox Code Playgroud)