【发布时间】: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 配置构建您的项目,它应该有助于获取异常的行信息。