在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → C# 文件编码转换工具(支持UTF-8/UTF-7/Unicode/ASCII/GB2312(简体中文)/BIG5 (繁体中文)等编码转换 )

C# 文件编码转换工具(支持UTF-8/UTF-7/Unicode/ASCII/GB2312(简体中文)/BIG5 (繁体中文)等编码转换 )

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:0.11M
  • 下载次数:65
  • 浏览次数:1385
  • 发布时间:2019-06-28
  • 实例类别:C#文件解析和处理
  • 发 布 人:wch11111111111
  • 文件格式:.zip
  • 所需积分:5
 相关标签: 转换 文件 编码 工具

实例介绍

【实例简介】

文件编码转换工具,批量转换文件到UTF8,支持自识别原格式,支持备份。


【实例截图】


from clipboard


from clipboard

【核心代码】


namespace FileEncodingTransform
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public class FormFileEncode : Form
    {
        private Button btnRemove;
        private Button btnSelectFiles;
        private Button button_Clipboard;
        private Button buttonBrowser;
        private Button buttonRun;
        private CheckBox chkIsBackup;
        private CheckBox chkUnknownEncoding;
        private ComboBox cmbSourceEncode;
        private ComboBox cmbTargetEncode;
        private IContainer components = null;
        private ArrayList ExtNameList;
        private FolderBrowserDialog folderBrowserDialog1;
        private string initPth = @"E:\Projects";
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private ListBox listSelectedFiles;
        private TextBox textBox_fileFilter;
        private TextBox txtResult;

        public FormFileEncode()
        {
            this.InitializeComponent();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.listSelectedFiles.SelectedIndex > -1)
            {
                this.listSelectedFiles.Items.RemoveAt(this.listSelectedFiles.SelectedIndex);
            }
        }

        private void btnSelectFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = true;
                dialog.InitialDirectory = this.initPth;
                if (!this.textBox_fileFilter.Text.Trim().Equals(""))
                {
                    dialog.Filter = this.textBox_fileFilter.Text.Trim();
                }
                if (DialogResult.OK == dialog.ShowDialog())
                {
                    string[] fileNames = dialog.FileNames;
                    foreach (string str in fileNames)
                    {
                        this.listSelectedFiles.Items.Add(str);
                    }
                }
            }
        }

        private void button_Clipboard_Click(object sender, EventArgs e)
        {
            string[] strArray = Clipboard.GetText().Split(new char[] { '\n' });
            string item = "";
            for (int i = 0; i < strArray.Length; i  )
            {
                item = strArray[i];
                if (item.IndexOf("\r") > -1)
                {
                    item = item.Replace("\r", "");
                }
                if (!item.Trim().Equals(""))
                {
                    this.listSelectedFiles.Items.Add(item);
                }
            }
        }

        private void buttonBrowser_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.SelectedPath = this.initPth;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.initPth = this.folderBrowserDialog1.SelectedPath;
                if (!this.textBox_fileFilter.Text.Trim().Equals(""))
                {
                    string[] strArray = this.textBox_fileFilter.Text.Trim().Split(new char[] { '|' });
                    if ((strArray.Length % 2) > 0)
                    {
                        MessageBox.Show("文件过滤字符串设置有误!");
                        return;
                    }
                    this.ExtNameList = new ArrayList();
                    for (int i = 0; i < (strArray.Length / 2); i  )
                    {
                        this.ExtNameList.Add(strArray[(i * 2)   1].Substring(strArray[(i * 2)   1].LastIndexOf("."), strArray[(i * 2)   1].Length - strArray[(i * 2)   1].LastIndexOf(".")).ToLower());
                    }
                }
                this.FindAllFiles(this.folderBrowserDialog1.SelectedPath);
            }
        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            this.txtResult.Text = "文件总数:"   this.listSelectedFiles.Items.Count.ToString()   "\r\n 正在执行......";
            int num = 0;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                num = 0;
                while (num < this.listSelectedFiles.Items.Count)
                {
                    this.ConvertFileEncode(this.listSelectedFiles.Items[num].ToString());
                    num  ;
                }
                this.txtResult.Text = this.txtResult.Text   "\r\n完成:"   this.listSelectedFiles.Items.Count.ToString();
            }
            catch (Exception exception)
            {
                string text = this.txtResult.Text;
                this.txtResult.Text = text   "\r\n执行文件:"   this.listSelectedFiles.Items[num].ToString()   "转换时出现错误:"   exception.Message   "\r\n已经完成:"   num.ToString();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void chkUnknownEncoding_CheckedChanged(object sender, EventArgs e)
        {
            this.cmbSourceEncode.Enabled = !this.chkUnknownEncoding.Checked;
        }

        private void ConvertFileEncode(string PathFile)
        {
            if (!PathFile.Trim().Equals(""))
            {
                Encoding selectEncoding;
                if (this.chkUnknownEncoding.Checked)
                {
                    IdentifyEncoding encoding2 = new IdentifyEncoding();
                    FileInfo testfile = new FileInfo(PathFile);
                    string name = string.Empty;
                    name = encoding2.GetEncodingName(testfile);
                    testfile = null;
                    if (name.ToLower() == "other")
                    {
                        this.txtResult.Text = this.txtResult.Text   string.Format("\r\n{0}文件格式不正确或已损坏。 ", PathFile);
                        return;
                    }
                    selectEncoding = Encoding.GetEncoding(name);
                }
                else
                {
                    selectEncoding = this.GetSelectEncoding(this.cmbSourceEncode.SelectedIndex);
                }
                string contents = File.ReadAllText(PathFile, selectEncoding);
                if (this.chkIsBackup.Checked)
                {
                    File.WriteAllText(PathFile   ".bak", contents, selectEncoding);
                }
                File.WriteAllText(PathFile, contents, this.GetSelectEncoding(this.cmbTargetEncode.SelectedIndex));
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FindAllFiles(string path)
        {
            string[] files = Directory.GetFiles(path);
            string str = "";
            int startIndex = 0;
            bool flag = false;
            foreach (string str2 in this.ExtNameList)
            {
                if (str2.Equals(".*"))
                {
                    flag = true;
                }
            }
            foreach (string str2 in files)
            {
                startIndex = str2.LastIndexOf(".");
                if (startIndex > -1)
                {
                    str = str2.Substring(startIndex, str2.Length - startIndex);
                }
                else
                {
                    str = "";
                }
                if (((this.ExtNameList == null) || (this.ExtNameList.IndexOf(str.ToLower()) > -1)) || flag)
                {
                    this.listSelectedFiles.Items.Add(str2);
                }
            }
            string[] directories = Directory.GetDirectories(path);
            foreach (string str2 in directories)
            {
                this.FindAllFiles(str2);
            }
        }

        private void FormFileEncode_Load(object sender, EventArgs e)
        {
            this.cmbSourceEncode.SelectedIndex = 4;
            this.cmbTargetEncode.SelectedIndex = 0;
        }

        private Encoding GetSelectEncoding(int i)
        {
            switch (i)
            {
                case 0:
                    return new UTF8Encoding(false);

                case 1:
                    return Encoding.UTF7;

                case 2:
                    return Encoding.Unicode;

                case 3:
                    return Encoding.ASCII;

                case 4:
                    return Encoding.GetEncoding(0x3a8);

                case 5:
                    return Encoding.GetEncoding("BIG5");
            }
            return new UTF8Encoding(false);
        }

        private void InitializeComponent()
        {
            this.buttonRun = new Button();
            this.buttonBrowser = new Button();
            this.cmbSourceEncode = new ComboBox();
            this.label1 = new Label();
            this.cmbTargetEncode = new ComboBox();
            this.label2 = new Label();
            this.label3 = new Label();
            this.folderBrowserDialog1 = new FolderBrowserDialog();
            this.btnRemove = new Button();
            this.txtResult = new TextBox();
            this.btnSelectFiles = new Button();
            this.listSelectedFiles = new ListBox();
            this.textBox_fileFilter = new TextBox();
            this.button_Clipboard = new Button();
            this.chkIsBackup = new CheckBox();
            this.chkUnknownEncoding = new CheckBox();
            this.label4 = new Label();
            base.SuspendLayout();
            this.buttonRun.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.buttonRun.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.buttonRun.Location = new Point(0x1da, 0x125);
            this.buttonRun.Name = "buttonRun";
            this.buttonRun.Size = new Size(0x2c, 0x17);
            this.buttonRun.TabIndex = 10;
            this.buttonRun.Text = "转换";
            this.buttonRun.UseVisualStyleBackColor = true;
            this.buttonRun.Click  = new EventHandler(this.buttonRun_Click);
            this.buttonBrowser.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.buttonBrowser.Location = new Point(420, 0x33);
            this.buttonBrowser.Name = "buttonBrowser";
            this.buttonBrowser.Size = new Size(0x60, 0x17);
            this.buttonBrowser.TabIndex = 9;
            this.buttonBrowser.Text = "按目录选文件";
            this.buttonBrowser.UseVisualStyleBackColor = true;
            this.buttonBrowser.Click  = new EventHandler(this.buttonBrowser_Click);
            this.cmbSourceEncode.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.cmbSourceEncode.AutoCompleteCustomSource.AddRange(new string[] { "UTF-8", "UTF-7", "Unicode", "ASCII", "GB2312(简体中文)", "BIG5 (繁体中文)" });
            this.cmbSourceEncode.FormattingEnabled = true;
            this.cmbSourceEncode.Items.AddRange(new object[] { "UTF-8", "UTF-7", "Unicode", "ASCII", "GB2312(简体中文)", "BIG5 (繁体中文)" });
            this.cmbSourceEncode.Location = new Point(420, 0xd9);
            this.cmbSourceEncode.Name = "cmbSourceEncode";
            this.cmbSourceEncode.Size = new Size(0x60, 20);
            this.cmbSourceEncode.TabIndex = 11;
            this.label1.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x1a3, 200);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x41, 12);
            this.label1.TabIndex = 12;
            this.label1.Text = "原文件编码";
            this.cmbTargetEncode.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.cmbTargetEncode.AutoCompleteCustomSource.AddRange(new string[] { "UTF-8", "UTF-7", "Unicode", "ASCII", "GB2312(简体中文)", "BIG5 (繁体中文)" });
            this.cmbTargetEncode.FormattingEnabled = true;
            this.cmbTargetEncode.Items.AddRange(new object[] { "UTF-8", "UTF-7", "Unicode", "ASCII", "GB2312(简体中文)", "BIG5 (繁体中文)" });
            this.cmbTargetEncode.Location = new Point(420, 0x108);
            this.cmbTargetEncode.Name = "cmbTargetEncode";
            this.cmbTargetEncode.Size = new Size(0x61, 20);
            this.cmbTargetEncode.TabIndex = 11;
            this.label2.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x1a3, 0xf7);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x41, 12);
            this.label2.TabIndex = 12;
            this.label2.Text = "转换后编码";
            this.label3.AutoSize = true;
            this.label3.Location = new Point(12, 9);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x59, 12);
            this.label3.TabIndex = 12;
            this.label3.Text = "文件过滤字符串";
            this.btnRemove.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnRemove.Location = new Point(0x1a5, 0x8d);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new Size(0x60, 0x17);
            this.btnRemove.TabIndex = 0x11;
            this.btnRemove.Text = "从列表中移除";
            this.btnRemove.UseVisualStyleBackColor = true;
            this.btnRemove.Click  = new EventHandler(this.btnRemove_Click);
            this.txtResult.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            this.txtResult.Location = new Point(15, 0x142);
            this.txtResult.Multiline = true;
            this.txtResult.Name = "txtResult";
            this.txtResult.ReadOnly = true;
            this.txtResult.ScrollBars = ScrollBars.Both;
            this.txtResult.Size = new Size(0x1f6, 0x58);
            this.txtResult.TabIndex = 0x10;
            this.txtResult.WordWrap = false;
            this.btnSelectFiles.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnSelectFiles.Location = new Point(0x1a5, 80);
            this.btnSelectFiles.Name = "btnSelectFiles";
            this.btnSelectFiles.Size = new Size(0x60, 0x17);
            this.btnSelectFiles.TabIndex = 15;
            this.btnSelectFiles.Text = "多选文件";
            this.btnSelectFiles.UseVisualStyleBackColor = true;
            this.btnSelectFiles.Click  = new EventHandler(this.btnSelectFiles_Click);
            this.listSelectedFiles.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.listSelectedFiles.FormattingEnabled = true;
            this.listSelectedFiles.HorizontalScrollbar = true;
            this.listSelectedFiles.ItemHeight = 12;
            this.listSelectedFiles.Location = new Point(15, 0x30);
            this.listSelectedFiles.Name = "listSelectedFiles";
            this.listSelectedFiles.Size = new Size(0x184, 0x10c);
            this.listSelectedFiles.TabIndex = 13;
            this.textBox_fileFilter.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.textBox_fileFilter.Location = new Point(0x6b, 7);
            this.textBox_fileFilter.Name = "textBox_fileFilter";
            this.textBox_fileFilter.Size = new Size(0x199, 0x15);
            this.textBox_fileFilter.TabIndex = 0x12;
            this.textBox_fileFilter.Text = "c文件|*.c|h文件|*.h";
            this.button_Clipboard.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.button_Clipboard.Location = new Point(420, 0x6d);
            this.button_Clipboard.Name = "button_Clipboard";
            this.button_Clipboard.Size = new Size(0x60, 0x17);
            this.button_Clipboard.TabIndex = 15;
            this.button_Clipboard.Text = "剪贴板中复制";
            this.button_Clipboard.UseVisualStyleBackColor = true;
            this.button_Clipboard.Click  = new EventHandler(this.button_Clipboard_Click);
            this.chkIsBackup.AutoSize = true;
            this.chkIsBackup.Checked = true;
            this.chkIsBackup.CheckState = CheckState.Checked;
            this.chkIsBackup.Location = new Point(0x1a5, 0x129);
            this.chkIsBackup.Name = "chkIsBackup";
            this.chkIsBackup.Size = new Size(0x30, 0x10);
            this.chkIsBackup.TabIndex = 20;
            this.chkIsBackup.Text = "备份";
            this.chkIsBackup.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.chkIsBackup.UseVisualStyleBackColor = true;
            this.chkUnknownEncoding.AutoSize = true;
            this.chkUnknownEncoding.Location = new Point(420, 0xb2);
            this.chkUnknownEncoding.Name = "chkUnknownEncoding";
            this.chkUnknownEncoding.Size = new Size(0x6c, 0x10);
            this.chkUnknownEncoding.TabIndex = 0x13;
            this.chkUnknownEncoding.Text = "自动识别原编码";
            this.chkUnknownEncoding.UseVisualStyleBackColor = true;
            this.chkUnknownEncoding.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.chkUnknownEncoding.CheckedChanged  = new EventHandler(this.chkUnknownEncoding_CheckedChanged);
            this.label4.AutoSize = true;
            this.label4.Location = new Point(0x69, 0x1f);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x17f, 12);
            this.label4.TabIndex = 0x15;
            this.label4.Text = "(坚线分隔,奇数为类型描述,偶数为扩展名。空值或*.*为全部文件)";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(530, 0x1a2);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.chkIsBackup);
            base.Controls.Add(this.chkUnknownEncoding);
            base.Controls.Add(this.textBox_fileFilter);
            base.Controls.Add(this.btnRemove);
            base.Controls.Add(this.txtResult);
            base.Controls.Add(this.button_Clipboard);
            base.Controls.Add(this.btnSelectFiles);
            base.Controls.Add(this.listSelectedFiles);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.cmbTargetEncode);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.cmbSourceEncode);
            base.Controls.Add(this.buttonRun);
            base.Controls.Add(this.buttonBrowser);
            base.Name = "FormFileEncode";
            base.ShowInTaskbar = true;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "文件编码转换工具 V1.1";
            base.Load  = new EventHandler(this.FormFileEncode_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}


实例下载地址

C# 文件编码转换工具(支持UTF-8/UTF-7/Unicode/ASCII/GB2312(简体中文)/BIG5 (繁体中文)等编码转换 )

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 564297091 发表于: 2022-04-12 11:28 12
无法实现,转换后,都是乱码

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警