gpt4 book ai didi

c# - 尝试从文件创建字符串

转载 作者:行者123 更新时间:2023-11-30 17:44:18 25 4
gpt4 key购买 nike

目前正在尝试从文本文件创建字符串,但它们似乎是一个错误,阻止流阅读器正确读取文本文件。

    private string testString = "Cheese";
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
fileName = openFileDialog.FileName;
LoadFile();
}
}

private void LoadFile()
{
String lineFromFile = "Chicken";
*StringBuilder RawFileInput = new StringBuilder();
using (StreamReader reader = new StreamReader(fileName))
{

while ((lineFromFile = reader.ReadLine()) != null)
{
RawFileInput.AppendLine(lineFromFile);
}
}*
testString = lineFromFile;
testTB.Text = testString;

}

如果代码执行的输出文本框为空,但是如果星号之间的代码块被注释掉,输出文本框显然会显示 Chicken 的测试短语。因此,我很确定这个特定 block 有问题,但我似乎无法弄清楚是什么。提前致谢。

最佳答案

如果我很好地理解了您的代码,那么您正在尝试使用文件中的文本设置 testTB.Text。考虑到这一点,你的最后一行不应该是:

    testString = RawFileInput.ToString(); 
testTB.Text = testString;

您可以在不需要 StringBuilder 的情况下获得相同的结果,用这一行替换整个 LoadFile 方法:

testTB.Text = File.ReadAllText(fileName);

关于c# - 尝试从文件创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808026/

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