C# 3.0 在文本文件中所做的更改(以编程方式)不会反映出来
我有以下代码。当我打开文件时,在文本文件中进行更改时,我看不到任何更改。事实上,文本文件的文本已被删除C# 3.0 在文本文件中所做的更改(以编程方式)不会反映出来,c#-3.0,C# 3.0,我有以下代码。当我打开文件时,在文本文件中进行更改时,我看不到任何更改。事实上,文本文件的文本已被删除 private void btnGo_Click(object sender, EventArgs e) { string content; string newword = "Tanu"; string oldword = "Sunrise"; System.IO.StreamReader file =
private void btnGo_Click(object sender, EventArgs e)
{
string content;
string newword = "Tanu";
string oldword = "Sunrise";
System.IO.StreamReader file =
new System.IO.StreamReader(txtFilePath.Text);
while ((content = file.ReadLine()) != null)
{
if (content == "Project name = Sunrise ")
{
string newtxt = Regex.Replace(content, oldword, newword);
content = content.Replace(content, newtxt);
}
// counter++;
}
file.Close();
using (StreamWriter writer = new StreamWriter(txtFilePath.Text))
{
writer.Write(content);
writer.Close();
错误在while循环中
while((content=file.ReadLine())!=null)
在用newText替换内容之后,while再次执行并尝试读取文件中的下一行,该行将内容设置为null,所以当您退出while循环时,您的内容为null
尝试将文件内容读入另一个变量,或者使用newText本身写入文件