开发者

“Index was outside the bounds of the array” when run outside of the IDE

I've just started learning C# and had created this simple configuration file generator for a Minecraft launcher, just as something to do. I'm having trouble when trying to run the application independently from Visual C# 2010 - it spits out "Index and length must refer to a location within the string. Parameter name:length" on first run (when the blank text file is created) and "index was outside the bounds of the array" if I start it again after this.

I'm confused as to why this is happening - it isn't giving me any line numbers, and it only happens when I don't run the application through the IDE (whether I run a debug build or release build). Any help is much appreciated. I apologise for my possibly horrible coding - I'm a beginner with this language.

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;
 开发者_JAVA百科           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)
        {

        }

    }
}


This code:

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

... will throw the exception you've talked about if the file contains fewer than 4 lines. Is that what's happening, perhaps? (You do talk about it creating a blank file.)

Note that you're also opening the file with a StreamReader but for no purpose, given that you're also just calling ReadAllLines which will open it separately.

When you write out the file with this code:

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

that's only writing out a single line, assuming outString doesn't contain any linebreaks... so I'm not sure how this could ever work, even in the IDE.)

(Note that if you do want to use StreamReader and StreamWriter, you should use using statements so that the file handle gets closed even if there are exceptions. You don't need to use StreamReader or StreamWriter here though - you could use File.WriteAllLines or File.WriteAllText.)

Finally, encryption should not be done over text. You will usually end up with garbled text which could easily contain invalid code units etc. Generally if you want to encrypt text, you convert it to binary, apply a standard encryption algorithm (don't write your own) and then if you want text out of it, you convert it to base64 with Convert.ToBase64String.


First of All dont Use StreamReader if you already used File.ReadAllLines it might close the File and make it Impossible for method File.readAllLines.

Try to load the Config File with the Full Path like

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

Than to be Sure test

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

And last if you wish to use Configuration than use Application.Setting


String.SubString will throw an ArgumentOutOfRangeException if
startIndex plus length indicates a position not within this instance.

During your first run you call this code:

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

username.Text.Substring(0, 1) is outside of the bounds of the array when you have an empty string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜