一个编码格式转换器

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WPA
{
    public partial class Form1 : Form
    {
        List<string> Paths;
        List<string> Exts;
        public Form1()
        {
            InitializeComponent();
            Paths = new List<string>();
            Exts = new List<string>();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            radF.Checked = true;
            tbxExts.Text = ".c;.cpp;.h;.dfm";
            string buf = tbxExts.Text;
            string[] sExts = buf.Split(';');
            for (int i = 0; i < sExts.Length; i++)
            {
                Exts.Add(sExts[i]);
            }
        }

        public string GB2312ToUTF8(string str)
        {
            try
            {
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                byte[] temp = gb2312.GetBytes(str);
                byte[] temp2 = Encoding.Convert(gb2312, Encoding.UTF8, temp);
                string result = Encoding.UTF8.GetString(temp2);
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool FileConvert(string FilePath)
        {
            string Buffer;
            bool bRet = false;
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            using (StreamReader sr = new StreamReader(FilePath, gb2312))
            {
                Buffer = sr.ReadToEnd();
            }

            //var utf8WithoutBom = new System.Text.UTF8Encoding(false);
            using (StreamWriter sw = new StreamWriter(FilePath, false, Encoding.UTF8))
            {
                //tbxLog.Text = Buffer;
                sw.Write(GB2312ToUTF8(Buffer));
                bRet = true;
            }
 
            return bRet;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            String FilePath;
            Paths.Clear();
            if(radF.Checked)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "cpp(*.cpp)|*.cpp";

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Paths.Add(ofd.FileName);
                }
            }
            else
            {
                FolderBrowserDialog fold = new FolderBrowserDialog();
                fold.Description = "选择待转换的文件路径";
                if (fold.ShowDialog() == DialogResult.OK)
                {
                    FilePath = fold.SelectedPath;
                    GetFiles(FilePath);
                }
            }
        }

        private void GetFiles(string Dir)
        {
            DirectoryInfo dir = new DirectoryInfo(Dir);
            FileInfo[] fileInfos = dir.GetFiles();
            for (int i = 0; i < fileInfos.Length; i++)
            {
                if(Exts.Contains(fileInfos[i].Extension))
                {
                    Paths.Add(fileInfos[i].FullName);
                }
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            tbxLog.Text = string.Empty;
            for (int i = 0; i < Paths.Count; i++)
            {
                if (FileConvert(Paths[i]))
                    tbxLog.Text += Paths[i] + "\r\n转换成功。\r\n";
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值