写入文件会出现方块

发布于 2024-12-03 08:37:20 字数 924 浏览 1 评论 0原文

对于我的测试数据,我使用以下字符:

漢字仮名交じり文

我写入文件的代码是:

// Create license key file
if (!File.Exists(Settings.LicenseFilesLocation + "/" + EmailAddress + "/" + LicenseFileName))
{
    HttpContext.Current.Response.Write(Name.Normalize());
    HttpContext.Current.Response.End();

    // UTF-8
    StreamWriter License = File.CreateText(Settings.LicenseFilesLocation + "/" + EmailAddress + "/" + LicenseFileName);

    License.WriteLine("Licensed to: " + Name.Normalize());

    License.Flush();
    License.Close();  

如您所见,我在将值保存到文件之前打印该值以进行检查,并且打印正确。但是,在保存的文本文件中,当我在记事本中打开它时,它会显示为很多方块(请参见屏幕截图):

输入图像描述此处

当我复制并粘贴这些字符时,它们会正确复制并粘贴为:

漢字仮名交じり文

我们有很多客户说他们的许可证文件中的名字位于他们不喜欢的正方形中。我们打开并验证该文件的软件程序也将其名称显示为正方形。

有什么想法如何解决这个问题吗?我们希望这样当客户打开他们的许可证 txt 文件时,它会显示他们的完整姓名,而不带方块。

For my test data I'm using the characters:

漢字仮名交じり文

My code that writes a file is:

// Create license key file
if (!File.Exists(Settings.LicenseFilesLocation + "/" + EmailAddress + "/" + LicenseFileName))
{
    HttpContext.Current.Response.Write(Name.Normalize());
    HttpContext.Current.Response.End();

    // UTF-8
    StreamWriter License = File.CreateText(Settings.LicenseFilesLocation + "/" + EmailAddress + "/" + LicenseFileName);

    License.WriteLine("Licensed to: " + Name.Normalize());

    License.Flush();
    License.Close();  

As you can see I'm printing the value before I save it to file to check it, and it is printing correctly. However in the saved text file when I open it in notepad, it's coming out as lots of squares (see screenshot):

enter image description here

When I copy and paste these characters they copy and paste correctly as:

漢字仮名交じり文

We are having a lot of customers saying their license file has their name in squares which they don't like. Our software program that opens and verifies this file is also showing their name as squares.

Any ideas how to fix this? We want it so that when customers open their license txt file it shows their name in full without squares.

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

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

发布评论

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

评论(1

梦行七里 2024-12-10 08:37:20

此问题有两个来源:

  1. 您的文件编码不正确。如果您使用 ASCII 字符以外的任何字符,那么您需要有意识地决定选择正确的编码。复制和粘贴有效的事实让我认为您的编码没问题,但您似乎仍然没有意识到这是在幕后发生的。
  2. 您的编辑器正在尝试使用无法正确呈现的字体来显示 unicode 字符。并非所有字体都支持 Unicode。将字体更改为可以呈现 Unicode 字符的字体,然后就可以开始了。

编辑:作为旁注,理解这个笑话这里也会对你有一些好处。

There are two sources to this problem:

  1. Your file is not encoded correctly. If you are using anything other than ASCII characters, then you need to make the conscious decision to choose the correct encoding. The fact that copying and pasting works makes me think that your encoding is OK, but you still seem unaware of the fact that this is occurring behind the scenes.
  2. Your editor is trying display unicode characters using a font that cannot render them correctly. Not every font supports Unicode. Change the font to a font that can render Unicode characters and you should be good to go.

EDIT: As a side note, understanding the joke here will also do you some good.

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