i18n 中 FileOpen 和 StringBuilder 的编码问题

发布于 2024-10-31 12:19:13 字数 813 浏览 5 评论 0原文

当我查看用 i18n 保存的文件时,这是可以的,例如其中有“Fïll Âll ülle~”,这就是我想要的..但​​是在代码中我试图读取此内容文件并将其作为字符串返回是这样的:

        string sLine = String.Empty;
        StringBuilder sHTMLText = new StringBuilder();
        int nFileHandle = FileSystem.FreeFile();

        sHTMLText.Append(String.Empty);
        FileSystem.FileOpen(nFileHandle, sFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1);

        while (!FileSystem.EOF(nFileHandle))
        {
            sLine = FileSystem.LineInput(nFileHandle);
            sHTMLText.Append(sLine);
        };
        FileSystem.FileClose(nFileHandle);
        return sHTMLText.ToString();

但是当我调试它时,它会破坏正确翻译的内容,例如“Fïll ¡ll ülle〜”并操纵它们,所以我认为我的方法没有以某种方式做到这一点尊重编码,就像我将计算机区域/语言设置设置为法语一样......那么我如何纠正我现有的代码或编写类似的东西,也关心我的计算机上的编码和语言设置?

谢克斯

when I look at my file that I have saved with i18n, it is ok for example there is "Fïll Âll ülle~" in it which is what I want..but the way in the code I am trying to read the contects of this file and return it as a String is some thing like that:

        string sLine = String.Empty;
        StringBuilder sHTMLText = new StringBuilder();
        int nFileHandle = FileSystem.FreeFile();

        sHTMLText.Append(String.Empty);
        FileSystem.FileOpen(nFileHandle, sFileName, OpenMode.Input, OpenAccess.Default, OpenShare.Default, -1);

        while (!FileSystem.EOF(nFileHandle))
        {
            sLine = FileSystem.LineInput(nFileHandle);
            sHTMLText.Append(sLine);
        };
        FileSystem.FileClose(nFileHandle);
        return sHTMLText.ToString();

but when I am debugging it, it is corrupting the correct translated stuff like "Fïll Âll ülle~" and manipulating them, so I think my method is not doing it in a way that honors Encoding like I have set my computer Regional/Language Settings to French .... so How can I correct my existing code or write something similar that also cares about Encoding and the lang set on my computer?

Thsanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冰魂雪魄 2024-11-07 12:19:13

查看 http://msdn.microsoft.com/en-us/library /ms143456.aspx 使用具有正确编码的 StreamReader。

马里奥

Have a look at http://msdn.microsoft.com/en-us/library/ms143456.aspx use a StreamReader with the correct encoding.

hth

Mario

别念他 2024-11-07 12:19:13

如果您尝试读取以非 Unicode 编码保存的文件,则必须在打开该文件时准确指定该编码。

using System;
using System.IO;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader reader = new StreamReader(@"C:\myfile.txt", Encoding.GetEncoding(1252)))
        {
            // read the file with the reader object.
        }

    }
}

一旦指定编码,文件在读取时将自动转换为内部字符串格式(UTF-16 LE)。请注意,如果正确指定了编码,则将旧字符编码中的有效文件转换为 Unicode 总是会成功且没有任何困难。以旧编码保存文件会出现更多问题,并且需要将所有源字符映射到旧编码或采用适当的后备机制。

将来在整个系统中专门使用 Unicode 将使事情变得更容易。依赖于正确设置的默认系统编码会创建隐藏的配置依赖项,这可能会在任何迁移、分布式应用程序和其他情况下导致问题。

If you are trying to read a file that was saved in a non-Unicode encoding, then you must specify exactly what that encoding was when you open the file.

using System;
using System.IO;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader reader = new StreamReader(@"C:\myfile.txt", Encoding.GetEncoding(1252)))
        {
            // read the file with the reader object.
        }

    }
}

Once you specify the encoding, then the file will automatically be translated into the internal string format (UTF-16 LE) when it is read. Note that the conversion of a valid file in a legacy character encoding into Unicode will always always succeed with no difficulties if the encoding is specified correctly. Saving a file in a legacy encoding is more problematic and requires that all of the source characters map to the legacy encoding or an appropriate fallback mechanism is in place.

Using Unicode exclusively throughout the system in the future will tend to make things easier going forward. Relying on the default system encoding to be set correctly creates a hidden configuration dependency that can cause problem during any migrations, distributed applications, and other circumstances.

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