現在我正在用c#構建一個遊戲應用程序,這將需要從遊戲腳本的文本文件中加載。 (這是一個相當簡單的視覺小說遊戲)用StringReader追加文本(reader.ReadLine()之後)
現在,當主窗體加載時,我加載該文件中的腳本script.txt 而且我宣佈:
StringReader reader = new StringReader(script);
爲全局變量有
現在在閱讀器處於字符串腳本中間的遊戲中間,我需要從閱讀器的下一行開始追加。 基本上我想達到的目標:
追加來自「news.txt」腳本所有文本從reader.ReadLine()即開始在字符串腳本的中間]
什麼是最有效的解決方案?
我所知道的:
StreamReader sr = new StreamReader("news.txt");
string news = sr.ReadToEnd();
//Now how to append 'news' to reader.ReadLine() ??
編輯更多的澄清(對不起,這是我第一次問這裏): 我會盡量解釋一下什麼,我想在這裏實現。 什麼,我有現在:
//global variables
string script;
StringReader reader;
//during form_load
StreamReader sr = new StreamReader("script.txt");
script = sr.ReadToEnd();
reader - new StringReader(script);
//And as the game progresses, I keep on implementing reader.ReadLine()..
//At one point, the program will ask the user, do you want to watch the news?
DialogResult dialogResult = MessageBox("Do you want to watch the news?", , MessageBoxButtons.YesNo
if(dialogResult == DialogResult.Yes)
{
StreamReader newsSr = new StreamReader("news.txt");
string news = newsSr.ReadToEnd();
//now I want to append the contents of 'news' to the string 'script' after reader.ReadLine() - any best way to implement this?
}
一種可能的方式(我認爲這是最糟糕的方式也一樣)是通過將一個計數變量,得到最後reader.ReadLine的起始位置() , 並執行所需的結果使用插入如下: script = script.Insert(startIndex,news)
閱讀器 - 讀取,寫入器 - 寫入 – sll
您確實需要附加到文件或「新聞」字符串嗎? –
+ sll,我找不到任何方法reader.write或這樣的?幫幫我? @Daniel我需要將字符串'news'追加到字符串'script'而不是實際的'script.txt' 我打算做的是追加到字符串腳本,reader = new StringReader(Script) ,然後再運行一個循環直到當前行。 –