StreamReader 未按预期工作
我编写了一个简单的实用程序,它循环遍历项目中的所有 C# 文件并更新顶部的版权文本。
例如,一个文件可能如下所示;
//Copyright My Company, © 2009-2010
程序应该将文本更新为如下所示;
//Copyright My Company, © 2009-2010
然而,我写的代码结果是这样的;
//Copyright My Company, � 2009-2011
这是我正在使用的代码;
public bool ModifyFile(string filePath, List<string> targetText, string replacementText)
{
if (!File.Exists(filePath)) return false;
if (targetText == null || targetText.Count == 0) return false;
if (string.IsNullOrEmpty(replacementText)) return false;
string modifiedFileContent = string.Empty;
bool hasContentChanged = false;
//Read in the file content
using (StreamReader reader = File.OpenText(filePath))
{
string file = reader.ReadToEnd();
//Replace any target text with the replacement text
foreach (string text in targetText)
modifiedFileContent = file.Replace(text, replacementText);
if (!file.Equals(modifiedFileContent))
hasContentChanged = true;
}
//If we haven't modified the file, dont bother saving it
if (!hasContentChanged) return false;
//Write the modifications back to the file
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(modifiedFileContent);
}
return true;
}
任何帮助/建议表示赞赏。谢谢!
I have written a simple utility that loops through all C# files in my project and updates the copyright text at the top.
For example, a file may look like this;
//Copyright My Company, © 2009-2010
The program should update the text to look like this;
//Copyright My Company, © 2009-2010
However, the code I have written results in this;
//Copyright My Company, � 2009-2011
Here is the code I am using;
public bool ModifyFile(string filePath, List<string> targetText, string replacementText)
{
if (!File.Exists(filePath)) return false;
if (targetText == null || targetText.Count == 0) return false;
if (string.IsNullOrEmpty(replacementText)) return false;
string modifiedFileContent = string.Empty;
bool hasContentChanged = false;
//Read in the file content
using (StreamReader reader = File.OpenText(filePath))
{
string file = reader.ReadToEnd();
//Replace any target text with the replacement text
foreach (string text in targetText)
modifiedFileContent = file.Replace(text, replacementText);
if (!file.Equals(modifiedFileContent))
hasContentChanged = true;
}
//If we haven't modified the file, dont bother saving it
if (!hasContentChanged) return false;
//Write the modifications back to the file
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(modifiedFileContent);
}
return true;
}
Any help/suggestions are appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个编码问题。
我认为您应该将此行更改
为使用正确编码保存的变体(看起来像这样的重载)
要获得正确的编码,请在打开文件的位置添加此行
This is an encoing problem.
I think you should change this line
To a variant that saves with the correct encoding (the overload that looks like this)
To get the correct encoding, where you have opened the file add this line
尝试使用
即
Try to use
i.e.
从阅读器获取编码并在编写器中使用它。
更改后的代码:
Get the Encoding from reader and use it in writer.
Changed code:
您必须指定编码
System.Text.Encoding.UTF8
应该执行以下操作诡计。整理好后请答应我阅读此内容。
You have to specify the encoding
System.Text.Encoding.UTF8
should do the trick.Once you've sorted it please promise me to read this.
我敢打赌这与文件内容的编码有关。确保使用正确的编码实例化 StreamWriter。 (http://msdn.microsoft.com/en-us/library/f5f5x7kt。 .aspx )
I'll bet it's related to the encoding of the file contents. Make sure you instantiate your StreamWriter with the correct encoding. ( http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx )