基本信息
源码名称:C# 文件编码转换工具(支持UTF-8/UTF-7/Unicode/ASCII/GB2312(简体中文)/BIG5 (繁体中文)等编码转换 )
源码大小:0.11M
文件格式:.zip
开发语言:C#
更新时间:2019-06-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍

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





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();
        }
    }
}