V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
547674115
V2EX  ›  C#

C#Encoding 文档编码的问题

  •  
  •   547674115 · 2018-12-22 13:00:22 +08:00 · 6495 次点击
    这是一个创建于 1923 天前的主题,其中的信息可能已经有所发展或是发生改变。

    弄了一个文本读写修改器,但是运行程序更改之后一直存在中文乱码问题,用 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 条回复    2018-12-22 14:58:35 +08:00
    GDC
        1
    GDC  
       2018-12-22 13:14:02 +08:00 via iPhone
    只要不超出编码范围的字符,无论存什么编码,打开的时候编码对应,就不会出现乱码。

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

    建议养成良好习惯,少用"GB2312",windows 改用 CP936,linux 改用 GBK
    只有 M$的东西(或浏览器)才把 GB2312 等同于 GBK,其实两者是有区别的,字符数量不是一个级别
    547674115
        3
    547674115  
    OP
       2018-12-22 13:35:06 +08:00
    @GDC 就是说我读文本的时候先指定一种编码,修改的时候也指定一种编码对吗?
    547674115
        4
    547674115  
    OP
       2018-12-22 13:42:51 +08:00 via iPhone
    @imn1 好的,了解了,方便给一些代码示例吗?
    imn1
        5
    imn1  
       2018-12-22 13:50:18 +08:00   ❤️ 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
       2018-12-22 13:53:59 +08:00
    @imn1 感谢!
    GDC
        7
    GDC  
       2018-12-22 14:08:38 +08:00 via iPhone
    @547674115 先用普通的文件编辑器打开你的文件,看看是什么编码的,C# 读的时候用同一个,保存的时候也用同一个。
    547674115
        8
    547674115  
    OP
       2018-12-22 14:58:35 +08:00 via iPhone
    @GDC Ok,已经搞定了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5488 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.