文本编码转化工具

最近一直在研究PHP 程序,对于文本文件经常需要转码。网上百度一下都没有合适的,自己动手只好自己动手写一个;这里放出来方便大家使用

 

 

程序源码

#region  文件描述信息
/********************************************************************************
** 作者: 刘志专
** 创始时间:2012-10-21
** 版权: 东莞东莞蓝鼎网络科技
** 描述:
** 文本编码转化工具
*********************************************************************************/
#endregion 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace EncodingChange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var encodings = Encoding.GetEncodings();
            List<string> items = new List<string>();
            items.AddRange(new List<string> { "UTF-8", "GB2312", "GBK", "UTF-16" });
            foreach (var obj in encodings)
            {
                if (!items.Contains(obj.Name.ToUpper()))
                {
                    items.Add(obj.Name);
                }
            }
            this.cbbNew.DataSource = items;

            this.cbbOld.DataSource = new List<string>(items);
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSourcePath.Text = dialog.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSavePath.Text = dialog.SelectedPath;
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (!System.IO.Directory.Exists(txtSourcePath.Text))
            {
                MessageBox.Show("请选择原文件目录");
                return;
            }
            if (!System.IO.Directory.Exists(txtSavePath.Text))
            {
                MessageBox.Show("请选择有效的文件保存目录");
                return;
            }
            Change(new System.IO.DirectoryInfo(txtSourcePath.Text), new System.IO.DirectoryInfo(txtSavePath.Text));
            MessageBox.Show("转换完成");
        }

        private void Change(System.IO.DirectoryInfo source, System.IO.DirectoryInfo saveDirectoryInfo)
        {
            foreach (var f in source.GetFiles())
            {
                string saveFileName = System.IO.Path.Combine(saveDirectoryInfo.FullName, f.Name);
                string content = System.IO.File.ReadAllText(f.FullName, Encoding.GetEncoding(cbbOld.Text));
                System.IO.File.WriteAllText(saveFileName, content, Encoding.GetEncoding(cbbNew.Text));
            }
            foreach (var dir in source.GetDirectories())
            {
                var name = System.IO.Path.Combine(saveDirectoryInfo.FullName, dir.Name);
                var newDir = System.IO.Directory.CreateDirectory(name);
                Change(dir, newDir);
            }
        }
    }
}

 

点击下面地址下载

文本编码转化工具下载

 

 

 

posted on 2012-10-21 21:53  上善至水  阅读(127)  评论(0)    收藏  举报

导航