如何获取命令行参数并将它们放入变量中

本文关键字:变量 何获 取命令行 参数 | 更新日期: 2023-09-27 18:12:07

我正在尝试做一个应用程序。有人可以帮助我如何获得命令行参数,并将它们放入变量/字符串。我需要在c#上这样做,它必须是5个参数。

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

谢谢你的帮助!

编辑:

我需要把这个添加到Windows窗体应用程序

如何获取命令行参数并将它们放入变量中

有两种方法。

第一种方法是使用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));
        }
    }
}

然后在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];
            }
        }
    }
}

另一种方法是使用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];
            }
        }
    }
}

,你会让Program.cs保持原来的样子,没有string[] args

考虑使用库来完成所有这些解析。我已经成功地使用了Github上的命令行解析器库:

https://github.com/commandlineparser/commandline

你可以使用Nuget获取这个库:

Install-Package CommandLineParser

下面是一个示例用法:

// 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);
    }
}

命令行参数在args数组中找到'

public static void Main(string[] args)
   {
       // The Length property is used to obtain the length of the array. 
       // Notice that Length is a read-only property:
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       for(int i = 0; i < args.Length; i++)
       {
           Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
       }
   }

源http://msdn.microsoft.com/en-us/library/aa288457 (v = vs.71) . aspx