- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚开始学习 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。)
(请注意,如果您确实想要使用StreamReader
和StreamWriter
,您应该使用using
语句,这样即使存在异常,文件句柄也会关闭。您不需要在此处使用 StreamReader
或 StreamWriter
- 您可以使用 File.WriteAllLines
或 File.WriteAllText
。)
最后,加密应该不对文本进行。您通常会得到乱码文本,其中很容易包含无效的代码单元等。通常,如果您想加密文本,您将其转换为二进制文件,应用标准加密算法(不要自己编写) 然后如果您想要其中的文本,您可以使用 Convert.ToBase64String
将其转换为 base64。
关于c# - 在 IDE 外部运行时为 “Index was outside the bounds of the array”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7569141/
嘿伙计们,我对java很陌生,我想知道是否有人可以解释为什么我在数组的开头得到一个随机空值,它是[null,r,b,o,e,r, t] 不知道为什么这样做!任何摆脱它的帮助都会很棒! public s
当我执行程序时 #include #include #include double exponential(double u); double exponential(double u) {
这个问题已经有答案了: 奥 git _a (17 个回答) 已关闭 7 年前。 我已经从 What does "Fatal error: Unexpectedly found nil while un
我正在开发一个债务跟踪应用程序,并且需要一个进度条,当当前债务下降时,进度会上升。我能想到的唯一方法是捏造周围的数字,使进度条 MAX 设置为 0,然后将起始债务更改为负数,如下所示 Progress
我知道当使用 ARC 并且你有一个 NSString 属性时,你可以像使用 MRC 一样执行 @property(nonatomic, copy)。但我想知道,在我将我的项目转换为 ARC 之后,我的
在 Xcode 6 Beta 2 上,我构建了一个带有 2 个 View Controller 的应用程序。 可以看到“Button”的 Action 是打开第二个ViewController。 但是
我正在使用 CreateFileA,我第一次调用它时,它按预期工作。但是当我第二次调用它时,它返回句柄 INVALID_HANDLE_VALUE。可能是什么问题呢?仅供引用,每次我需要检查我的 USB
我有一个被映射到生成器中的增强函数;然而,出于某种原因,tfa.image.rotate函数导致错误。 def customGenerator(input_file_paths, dims, data
我有查询并且有效 select *, id, from_unixtime(start_timestamp) from log limit 1; 这也很好用: select * from log lim
我有一个 FutureBuilder在我的具有常规结构的应用程序中: FutureBuilder( future: futureData(), builder: (context, snaps
所以我最近才接触到C++的SDL2,我做了这个非常简单的代码: int main(int argc, char** argv) { SDL_Event *event; bool done = fals
我正在打包一个非常简单的 redmine 插件 rpm,以供内部使用,但我遇到了“Package has no %description:”错误。 .spec 有一个描述,但我在 inet 上找不到任
call main() sub main() Dim scmd Set scmd = "c:\windows\system32\cscript.exe //nologo c:\s.vb
我正在使用以下代码在 Java 应用程序中使用 Git。我有一个有效的 key (一直使用它),并且这个特定的代码之前使用相同的 key 和 git 存储库对我有用,但现在我得到以下异常: inval
我有一个 Java 小程序,可以查询 Oracle 数据库中的数据。当从 IDE 内部运行时,它运行得很好。但是,当我将它作为嵌入网页中的小程序运行时,我在类加载器中收到“访问被拒绝”错误,并且我根本
我正在将一个 HTML 文件从 MainBundle 加载到 UIWebview 中,并想传入一个查询字符串,因为 HTML 文件检查 GET 参数 (?device=1) 以进行一些额外的计算。我还
我有一个包含多个项目的解决方案。每个项目都有自己的 nuspec 文件,其中一些项目相互引用 当我通过调用创建一个新包时 nuget pack MyLibrary.csproj -IncludeRef
我有一个测试文件和一个主模块文件,其中有一个我正在测试的函数。在测试文件的末尾,我有 unittest.main() 来运行单元测试。但是,当我运行测试文件时,控制台显示“未找到测试”,即使我的文件中
我在构建 docker 镜像时使用 dotnet:2.1-sdk 作为包。RUN dotnet restore 命令失败并出现以下错误: **error : Unable to load the se
这是一个简单的程序,只会执行 3 个子进程。每个都将创建一个锁,读取一个文本文件,并取消该锁的链接。如果正确执行,子进程将退出,并将其进程 ID 转换为 8 位。如果无法创建/取消链接锁,程序将尝试
我是一名优秀的程序员,十分优秀!