547674115
V2EX  ›  C#

C#Encoding 文档编码的问题

  •  
  •   547674115 · Dec 22, 2018 · 7337 views
    This topic created in 2699 days ago, the information mentioned may be changed or developed.

    弄了一个文本读写修改器,但是运行程序更改之后一直存在中文乱码问题,用 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\"");
                }
            }
        }
    }
    

    }]

    8 replies    2018-12-22 14:58:35 +08:00
    GDC
        1
    GDC  
       Dec 22, 2018 via iPhone
    只要不超出编码范围的字符,无论存什么编码,打开的时候编码对应,就不会出现乱码。

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

    建议养成良好习惯,少用"GB2312",windows 改用 CP936,linux 改用 GBK
    只有 M$的东西(或浏览器)才把 GB2312 等同于 GBK,其实两者是有区别的,字符数量不是一个级别
    547674115
        3
    547674115  
    OP
       Dec 22, 2018
    @GDC 就是说我读文本的时候先指定一种编码,修改的时候也指定一种编码对吗?
    547674115
        4
    547674115  
    OP
       Dec 22, 2018 via iPhone
    @imn1 好的,了解了,方便给一些代码示例吗?
    imn1
        5
    imn1  
       Dec 22, 2018   ❤️ 1
    我没写过 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
        6
    547674115  
    OP
       Dec 22, 2018
    @imn1 感谢!
    GDC
        7
    GDC  
       Dec 22, 2018 via iPhone
    @547674115 先用普通的文件编辑器打开你的文件,看看是什么编码的,C# 读的时候用同一个,保存的时候也用同一个。
    547674115
        8
    547674115  
    OP
       Dec 22, 2018 via iPhone
    @GDC Ok,已经搞定了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 02:53 · PVG 10:53 · LAX 19:53 · JFK 22:53
    ♥ Do have faith in what you're doing.