C#Encoding 文档编码的问题

2018-12-22 13:00:22 +08:00
 547674115

弄了一个文本读写修改器,但是运行程序更改之后一直存在中文乱码问题,用 NOTEpad++ 打开显示是 GB2312 但是用 Encoding.Default,Encoding.GetEncoding("uft-8"),Encoding.GetEncoding("GB2312")都试过了,但是都不行,麻烦各位指点一二。

代码,贴上 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;

namespace frmModify { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private List<string> lines;</string>

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
       
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            txtPath.Text = dialog.SelectedPath;
        }
    }

    private void btnModify_Click(object sender, EventArgs e)
    {

        if(txtPath.Text == string.Empty)
        {
            MessageBox.Show("请选择文件");
            return;
        }

        string[] files = System.IO.Directory.GetFiles(txtPath.Text, "*.xml", SearchOption.AllDirectories);

        foreach(string file in files)
        {
            LoadFile(file);
            ReplaceText();
            SaveFile(file);
        }
        MessageBox.Show("替换成功");
    }
    private void LoadFile(string file)
    {
        lines = System.IO.File.ReadAllLines(file).ToList();
    }
    

    private void SaveFile(string file)
    {
        
        System.IO.File.SetAttributes(file, FileAttributes.Normal);
        System.IO.File.WriteAllLines(file, lines, Encoding.GetEncoding("GB2312"));
    }

    private void ReplaceText()
    {
        foreach (string line in lines)
        {
            
            if (line.IndexOf("删除单据") > 0)
            {
                line.Replace("Type=\"Edit\"", "Type=\"Print\"");
            }
        }
    }
}

}]

6536 次点击
所在节点    C#
8 条回复
GDC
2018-12-22 13:14:02 +08:00
只要不超出编码范围的字符,无论存什么编码,打开的时候编码对应,就不会出现乱码。

同理,C# 在打开你现有文件的时候,也要指定一致的编码,不然它读到的就是乱码了,保存之后当然也是乱码。
imn1
2018-12-22 13:34:33 +08:00
同上,没看到读入时编码的判断,也没看到转码的操作
单纯读入就指定一个编码写入?

建议养成良好习惯,少用"GB2312",windows 改用 CP936,linux 改用 GBK
只有 M$的东西(或浏览器)才把 GB2312 等同于 GBK,其实两者是有区别的,字符数量不是一个级别
547674115
2018-12-22 13:35:06 +08:00
@GDC 就是说我读文本的时候先指定一种编码,修改的时候也指定一种编码对吗?
547674115
2018-12-22 13:42:51 +08:00
@imn1 好的,了解了,方便给一些代码示例吗?
imn1
2018-12-22 13:50:18 +08:00
我没写过 C#,不过写过 powershell,也是调用.net ,差不多吧,看着改

$stream=$response.GetResponseStream() $enc=[System.Text.ASCIIEncoding]::GetEncoding(20932)
$encu = [System.Text.Encoding]::UTF8
$en_stream=[System.IO.StreamReader]::New($stream,$enc)
$data1 = $en_stream.ReadToEnd()
$ss = [System.Text.Encoding]::Convert($enc, $encu, $enc.GetBytes($data1))
$data = $encu.getstring($ss)

$response 是网页来源
20932 是一个日文编码
547674115
2018-12-22 13:53:59 +08:00
@imn1 感谢!
GDC
2018-12-22 14:08:38 +08:00
@547674115 先用普通的文件编辑器打开你的文件,看看是什么编码的,C# 读的时候用同一个,保存的时候也用同一个。
547674115
2018-12-22 14:58:35 +08:00
@GDC Ok,已经搞定了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/519981

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX