StreamReader 未按预期工作

发布于 2024-10-11 21:10:41 字数 1427 浏览 4 评论 0原文

我编写了一个简单的实用程序,它循环遍历项目中的所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月静好 2024-10-18 21:10:41

这是一个编码问题。

我认为您应该将此行更改

using (StreamWriter writer = new StreamWriter(filePath))

为使用正确编码保存的变体(看起来像这样的重载)

using (StreamWriter writer = new StreamWriter(filePath, false, myEncoding))

要获得正确的编码,请在打开文件的位置添加此行

myEncoding = reader.CurrentEncoding;

This is an encoing problem.

I think you should change this line

using (StreamWriter writer = new StreamWriter(filePath))

To a variant that saves with the correct encoding (the overload that looks like this)

using (StreamWriter writer = new StreamWriter(filePath, false, myEncoding))

To get the correct encoding, where you have opened the file add this line

myEncoding = reader.CurrentEncoding;
乖乖哒 2024-10-18 21:10:41

尝试使用

StreamWriter(string path, bool append, Encoding encoding)

new StreamWriter(filePath, false, new UTF8Encoding())

Try to use

StreamWriter(string path, bool append, Encoding encoding)

i.e.

new StreamWriter(filePath, false, new UTF8Encoding())
同展鸳鸯锦 2024-10-18 21:10:41

从阅读器获取编码并在编写器中使用它。

更改后的代码:

public bool ModifyFile(string filePath, List 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;
    Encoding sourceEndocing = null;

    using (StreamReader reader = File.OpenText(filePath))
    {
        sourceEndocing = reader.CurrentEncoding;
        string file = reader.ReadToEnd();

        foreach (string text in targetText)
            modifiedFileContent = file.Replace(text, replacementText);

        if (!file.Equals(modifiedFileContent))
            hasContentChanged = true;
    }

    if (!hasContentChanged) return false;

    using (StreamWriter writer = new StreamWriter(filePath, false, sourceEndocing))
    {
        writer.Write(modifiedFileContent);
    }

    return true;
}

Get the Encoding from reader and use it in writer.

Changed code:

public bool ModifyFile(string filePath, List 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;
    Encoding sourceEndocing = null;

    using (StreamReader reader = File.OpenText(filePath))
    {
        sourceEndocing = reader.CurrentEncoding;
        string file = reader.ReadToEnd();

        foreach (string text in targetText)
            modifiedFileContent = file.Replace(text, replacementText);

        if (!file.Equals(modifiedFileContent))
            hasContentChanged = true;
    }

    if (!hasContentChanged) return false;

    using (StreamWriter writer = new StreamWriter(filePath, false, sourceEndocing))
    {
        writer.Write(modifiedFileContent);
    }

    return true;
}
飘逸的'云 2024-10-18 21:10:41

您必须指定编码

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.

み格子的夏天 2024-10-18 21:10:41

我敢打赌这与文件内容的编码有关。确保使用正确的编码实例化 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 )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文