![](/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.