【问题标题】:“Index was outside the bounds of the array” when run outside of the IDE在 IDE 之外运行时,“索引超出了数组的范围”
【发布时间】:2011-11-26 00:23:47
【问题描述】:

我刚刚开始学习 C#,并为 Minecraft 启动器创建了这个简单的配置文件生成器,就像要做的事情一样。我在尝试独立于 Visual C# 2010 运行应用程序时遇到问题 - 它吐出 “索引和长度必须引用字符串中的位置。参数名称:长度” 在第一次运行时(当空白文本文件已创建)并且 “索引超出了数组的范围” 如果我在此之后再次启动它。

我对为什么会发生这种情况感到困惑 - 它没有给我任何行号,而且只有当我不通过 IDE 运行应用程序时才会发生这种情况(无论我运行的是调试版本还是发布版本) .任何帮助深表感谢。对于我可能糟糕的编码,我深表歉意 - 我是这门语言的初学者。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Minecraft_Launcher_Config
{

    public partial class mcwindow : Form
    {

        private void outputText(string outtext)
        {
            output.Text = outtext;
        }

        private string encrypt(string text, int key)
        {
            string newText = "";

            for (int i = 0; i < text.Length; i++)
            {
                int charValue = Convert.ToInt32(text[i]);
                charValue ^= key;

                newText += char.ConvertFromUtf32(charValue);
            }

            return newText;
        }
        public mcwindow()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string iuser = "";
            string ipass = "";
            string imem = "1024";
            string iserver = "";
            if (System.IO.File.Exists("config.cfg"))
            {
                System.IO.StreamReader configFile =
                new System.IO.StreamReader("config.cfg");
                string[] iconfig = System.IO.File.ReadAllLines("config.cfg");
                iuser = iconfig[0];
                ipass = iconfig[1];
                imem = iconfig[2];
                iserver = iconfig[3];
                configFile.Close();
                outputText("Successfully loaded and decrypted config!");
            } else
            {
                System.IO.StreamWriter configwrite = new System.IO.StreamWriter("config.cfg");
                configwrite.Close();
                outputText("Welcome to Frohman's Minecraft Launcher Config!");
            }

            username.Text = iuser;
            memselect.Value = int.Parse(imem);
            int OKey = Convert.ToInt32(username.Text.Substring(0, 1));
            string unenpassword = encrypt(ipass, OKey);
            password.Text = unenpassword;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void saveexit_Click(object sender, EventArgs e)
        {
            if (username.Text != "" & password.Text != "")
            {
                outputText("Saving and exiting!");
                int IKey = Convert.ToInt32(username.Text[0]);
                string enpassword = encrypt(password.Text, IKey);
                string outString = username.Text + System.Environment.NewLine + enpassword + System.Environment.NewLine + memselect.Value + System.Environment.NewLine + serverip.Text;
                System.IO.StreamWriter configwrite = new System.IO.StreamWriter("config.cfg");
                configwrite.WriteLine(outString);
                configwrite.Close();
                System.Threading.Thread.Sleep(150);
                Environment.Exit(0);
            }
            else
            {
                outputText("You're missing some data!");
            }
        }

        private void password_TextChanged(object sender, EventArgs e)
        {

        }

    }
}

【问题讨论】:

  • 确保您的 config.cfg 文件有 4 行
  • 顺便说一句。尝试使用 Debug 配置构建您的项目,它应该有助于获取异常的行信息。

标签: c# exception


【解决方案1】:

这段代码:

    string[] iconfig = System.IO.File.ReadAllLines("config.cfg");
    iuser = iconfig[0];
    ipass = iconfig[1];
    imem = iconfig[2];
    iserver = iconfig[3];

... 如果文件包含少于 4 行,则会抛出您所说的异常。这可能是正在发生的事情吗? (您确实谈到它会创建一个空白文件。)

请注意,您还使用StreamReader 打开文件,但没有任何目的,因为您也只是调用ReadAllLines,它将单独打开它。

当你用这段代码写出文件时:

System.IO.StreamWriter configwrite = new System.IO.StreamWriter("config.cfg");
configwrite.WriteLine(outString);
configwrite.Close();

这只是写出一行,假设 outString 不包含任何换行符...所以我不确定这 ever 是如何工作的,即使在 IDE 中也是如此。)

(请注意,如果您确实想使用StreamReaderStreamWriter,则应使用using 语句,这样即使出现异常,文件句柄也会关闭。您不要'不过这里不需要使用StreamReaderStreamWriter - 你可以使用File.WriteAllLinesFile.WriteAllText。)

最后,不应对文本进行加密。您通常会得到乱码文本,其中很容易包含无效的代码单元等。通常,如果您想加密文本,请将其转换为二进制,应用 标准 加密算法(不要自己编写) 然后如果你想要文本,你可以使用Convert.ToBase64String将它转换为base64。

【讨论】:

  • 感谢您的反馈,它对我帮助很大!少于四行的问题很有可能,我似乎忽略了。读写方法的混乱是我测试各种方法的可怕结果。对于它的价值,换行符包含在输出文本中,但是我确实从看似无处获得了一个额外的换行符。简单的加密又是一个测试,不过感谢您提供的信息!很有帮助!
【解决方案2】:

如果您已经使用过File.ReadAllLines,首先不要使用StreamReader 它可能会关闭文件并使其无法用于方法File.readAllLines.

尝试使用完整路径加载配置文件,如

string[] iconfig = System.IO.File.ReadAllLines(@"MyPath\config.cfg");

比确定测试

if(iconfig.length >= 4)
    //Proceed With Config

最后,如果您希望使用配置而不是使用 Application.Setting

【讨论】:

  • 非常感谢!我正在清理文件读取机制,因为它们到处都是。关于路径 - 我从来没有打算让配置文件出现在 .exe 旁边以外的任何地方。
  • @Frohman 它可能在您的 .exe 中,您应该使用“/config.cfg”访问它,否则某些系统将开始搜索 config.cfg 和/或使用 config.cfg 文件这是在系统变量的配置。
【解决方案3】:

如果
String.SubString 将抛出 ArgumentOutOfRangeException startIndex 加上长度表示不在此实例中的位置。

在您第一次运行时,您调用此代码:

    iuser="";
    ...
    username.Text = iuser;
    memselect.Value = int.Parse(imem);
    int OKey = Convert.ToInt32(username.Text.Substring(0, 1));

当您有一个空字符串时,username.Text.Substring(0, 1) 超出了数组的范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2015-11-06
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多