![](/img/trans.png)
[英]How to read from a text file that is constantly updating and write to a TextBlock in Visual Studio? [on hold]
[英]Read from text file with StringReader in Visual Studio
我正在制作一个小型益智游戏,并尝试使用StringReader从文本文件中将一些信息加载到字符串中。 它将加载到数据网格视图。 该文本文件名为TextFile1.txt,位于名为Puzzles的文件夹中。 文本文件设置为始终复制到输出目录。
该项目将生成,但不会将其加载到数据网格视图中。 文本文件内容如下
x|y|direction|number|word|clue
5|5|down|1|love|Let _____ Rule
4|5|across|2|closed|Not Open
5|8|across|3|eraser|At the other end of a pencil
10|8|down|2|red|Hunt for _____ October
10|10|across|4|dallas|Redskin rival's city
9|5|down|3|dare|Triple Dog
13|8|down|4|relapse|To succumb again
11|12|across|5|cap|A night ____
码
Clues clue_window = new Clues();
List<id_cells> idc = new List<id_cells>();
public String puzzle_file = Application.StartupPath + "\\Puzzles\\TextFile1.txt";
public Form1()
{
buildWordList();
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buildWordList()
{
String line = "";
using (StringReader s = new StringReader(puzzle_file))
{
s.ReadToEnd();
line = s.ReadLine();//ignores the first line
while((line = s.ReadLine()) != null)
{
String[] l = line.Split('|');
idc.Add(new id_cells(Int32.Parse(l[0]), Int32.Parse(l[1]), l[2], l[3], l[4], l[5]));
clue_window.clue_table.Rows.Add(new String[] { l[3], l[2], l[5] });
}
}
}
首先,当您遇到此类问题时,请首先检查Reader的路径(我建议您使用StreamReader而不是StringReader)。
从我的角度来看,可能有两个问题:
1,由于找不到txt文件,因此读取可能不起作用(在该循环中输入时请显示一个messageBox框以进行检查)
要么
2.s.ReadToEnd从流中读取所有内容。
该函数从文件中复制所有数据(从第一个字节到最后一个字节)。 因此,下次您阅读一行时,它将返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.