【问题标题】:C# "Command-Line arguments exception"C#“命令行参数异常”
【发布时间】:2017-08-22 14:57:37
【问题描述】:

如何将字符串命令行参数存储到自定义字符串变量中。因为它倾向于给出“数组索引超出范围边界”......

这是代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prac1_e
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a String and a number : ");
            Console.Read();
            string str = args[0];
            int n = Convert.ToInt32(args[1]);
            Console.WriteLine(str);
            Console.WriteLine(n);
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 请显示您输入的输入字符串
  • 您是否将参数传递给应用程序?
  • 您正在阅读arg,当您在命令行中使用参数执行程序时,它具有价值。当您需要运行时参数时,您必须读取 Console.Read(); 本身。它的返回类型为字符串。
  • @praty 很好。修改了我的答案以包括这两个选项。
  • @praty " 它的返回类型是字符串" 抱歉,但不,它的返回类型是 int 并且它只从输入流中读取下一个字符。可能你的意思是ReadLine ?=!

标签: c# command-line-arguments


【解决方案1】:

您正在尝试访问前两个命令行参数(agrs[0]args[1]),但没有传递任何参数。只需传递一些参数就可以了:

Program somearg anotherarg

【讨论】:

    【解决方案2】:

    这段代码没有错误。但是,当您错误地执行程序时,您会遇到上述错误。 这应该是正确的方法:

    [program name] [argument 1] [argument 2] 
    

    例如:

    testprogram arg1 5
    

    【讨论】:

      【解决方案3】:

      参数需要通过命令行以appname.exe "arg1" "arg2" 等格式传递。此外,由于您要转换为Int,您可能需要验证arg[1] 是否可转换为Int

          static void Main(string[] args)
          {
              Console.WriteLine("Enter a String and a number : ");
              Console.Read();
              if(args.Length >= 2)
              {
                  string str = args[0];
                  int n = Convert.ToInt32(args[1]);
                  Console.WriteLine(str);
                  Console.WriteLine(n);
                  Console.ReadKey();
              }
              else
              {
                  Console.WriteLine("No Args passed");
              }
          }
      

      如果您希望通过命令行捕获运行时参数,则需要执行以下操作:

          static void Main(string[] args)
          {
              Console.WriteLine("Enter a String and a number, with a space between: ");
              string consoleRead = Console.ReadLine();
              string[] parsed = consoleRead.Split(' ');
              if (parsed.Length > 1)
              {
                  string str = parsed[0];
                  int n = Convert.ToInt32(parsed [1]);
                  Console.WriteLine(str);
                  Console.WriteLine(n);
                  Console.ReadKey();
              }
          }
      

      【讨论】:

        【解决方案4】:

        如果您打算从命令行读取运行时传递的参数,则需要从 Console 对象读取。你可以试试这个代码。

        static void Main(string[] args)
            {
                Console.WriteLine("Enter a String and a number : ");
        
                string str = Console.ReadLine();
                int n = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(str);
                Console.WriteLine(n);
                Console.ReadKey();
            }
        

        【讨论】:

        • 我会让@KamleshSharma 确认这是他想要实现的目标,还是他真的想阅读命令行参数。
        • 编辑了我的答案。希望这比以前更具描述性。
        • 绝对可以提高您的回答质量。 :)
        猜你喜欢
        • 2012-08-25
        • 2017-06-26
        • 2016-12-22
        • 2013-10-09
        • 1970-01-01
        • 2015-04-03
        • 2013-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多