gpt4 book ai didi

c# - 在 IDE 外部运行时为 “Index was outside the bounds of the array”

转载 作者:行者123 更新时间:2023-11-30 16:27:46 25 4
gpt4 key购买 nike

我刚开始学习 C# 并为 Minecraft 启动器创建了这个简单的配置文件生成器,只是为了做点什么。我在尝试独立于 Visual C# 2010 运行应用程序时遇到了问题 - 它吐出 "Index and length must refer to a location within the string. Parameter name:length" 在第一次运行时(当创建了空白文本文件)和“索引超出了数组的范围”如果我在这之后再次启动它。

我很困惑为什么会发生这种情况 - 它没有给我任何行号,而且只有当我不通过 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)
{

}

}
}

最佳答案

这段代码:

    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 不包含任何换行符...所以我不确定这如何曾经工作,即使在IDE。)

(请注意,如果您确实想要使用StreamReaderStreamWriter,您应该使用using 语句,这样即使存在异常,文件句柄也会关闭。您不需要在此处使用 StreamReaderStreamWriter - 您可以使用 File.WriteAllLinesFile.WriteAllText。)

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

关于c# - 在 IDE 外部运行时为 “Index was outside the bounds of the array”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7569141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com