C#编写项目编码格式转换器
本人是JAVA开发 由于开发的人多了 有的人开发环境编码格式也不同 有的是GBK 有的是UTF-8 代码中更是层出不穷
直接导致项目中中文的类乱码 写代码不能加注释的囧地 为了解决此问题编写了此软件 ?分享出来希望能帮助到其他人
部分代码如下:
?
public int[] Count=new int[3];//存储计数信息 public string SelectPath = null;//存储选择的路径 private Profile profile = new Profile(); string confitemp = "Configuration";//配置的总节点 /// <summary> /// 判断文件编码格式是什么 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static Encoding GetEncoding(FileStream stream) { Encoding targetEncoding = Encoding.Default; if (stream != null && stream.Length >= 2) { //保存文件流的前4个字节 byte byte1 = 0; byte byte2 = 0; byte byte3 = 0; byte byte4 = 0; //保存当前Seek位置 long origPos = stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin); int nByte = stream.ReadByte(); byte1 = Convert.ToByte(nByte); byte2 = Convert.ToByte(stream.ReadByte()); if (stream.Length >= 3) { byte3 = Convert.ToByte(stream.ReadByte()); } if (stream.Length >= 4) { byte4 = Convert.ToByte(stream.ReadByte()); } //根据文件流的前4个字节判断Encoding //Unicode {0xFF, 0xFE}; //BE-Unicode {0xFE, 0xFF}; //UTF8 = {0xEF, 0xBB, 0xBF}; if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe { targetEncoding = Encoding.BigEndianUnicode; } if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode { targetEncoding = Encoding.Unicode; } if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8 { targetEncoding = Encoding.UTF8; } //恢复Seek位置 stream.Seek(origPos, SeekOrigin.Begin); } return targetEncoding; } /// <summary> /// 通过给定的文件流,判断文件的编码类型 /// </summary> /// <param name="fs">文件流</param> /// <returns>文件的编码类型</returns> public Encoding GetType(FileStream fs) { byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM Encoding reVal = Encoding.Default; BinaryReader r = new BinaryReader(fs,Encoding.Default); int i; int.TryParse(fs.Length.ToString(), out i); byte[] ss = r.ReadBytes(i); if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)) { reVal = Encoding.UTF8; } else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00) { reVal = Encoding.BigEndianUnicode; } else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41) { reVal = Encoding.Unicode; } r.Close(); return reVal; } /// <summary> /// 判断是否是不带 BOM 的 UTF8 格式 /// </summary> /// <param name="data"></param> /// <returns></returns> private bool IsUTF8Bytes(byte[] data) { int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 byte curByte; //当前分析的字节. for (int i = 0; i < data.Length; i++) { curByte = data[i]; if (charByteCounter == 1) { if (curByte >= 0x80) { //判断当前 while (((curByte <<= 1) & 0x80) != 0) { charByteCounter++; } //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X if (charByteCounter == 1 || charByteCounter > 6) { return false; } } } else { //若是UTF-8 此时第一位必须为1 if ((curByte & 0xC0) != 0x80) { return false; } charByteCounter--; } } if (charByteCounter > 1) { throw new Exception("非预期的byte格式"); } return true; } private void butttrun_Click(object sender, EventArgs e) { this.butttrun.Enabled = false; this.buttonSelect.Enabled = false; Count = new int[3];//存储计数信息 if (SelectPath != null) { this.Tag = this.Text; this.Text = "正在努力拷贝项目请稍候....(请勿关闭本软件)"; List<string> files = GetFileName(SelectPath);//要转码的所有文件 int copycount = GetCopyFileCount(SelectPath, files); this.Text = "正在努力转码 请稍候....(请勿关闭本软件)"; foreach (var sub in files) { Transcoding(sub); } this.Text = "" + this.Tag; LogBLL.Err("转码总计: 成功" + Count[0] + "个 失败 " + Count[1] + "个 不存在 " + Count[2] + "个 拷贝" + copycount + "个"); MessageBox.Show(this, "转换文件总计: 成功" + Count[0] + "个 失败 " + Count[1] + "个 不存在 " + Count[2] + "个 拷贝" + copycount + "个", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } else { MessageBox.Show(this, "请先选择您要处理的文件", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } this.butttrun.Enabled = true ; this.buttonSelect.Enabled = true; } /// <summary> /// 获取资源文件所有文件 /// </summary> /// <param name="SelectPath"></param> /// <returns></returns> public int GetCopyFileCount(string SelectPath, List<string> files) { int i = 0; string[] fileName = Directory.GetFiles(SelectPath, "*.*", SearchOption.AllDirectories); foreach (var file in fileName) { if (!files.Contains(file)) { try { int lastlen = file.LastIndexOf("\");//结束的/ int startlen = file.IndexOf("\");//开始的/ string temp = file.Substring(lastlen + 1); string tempqian = file.Substring(startlen + 1, (lastlen - startlen) - 1); Generate(tempqian);//生成一个文件夹 File.Copy(file, System.Environment.CurrentDirectory + "\" + tempqian + "\" + temp, true); i++; } catch (Exception e) { LogBLL.Err("文件[" + file + "]拷贝失败[" + e .Message+ "]"); } } } return i; } /// <summary> /// 转码指定文件转换为固定编码格式 /// </summary> public void Transcoding(string filename) { Encoding en2 = Encoding.Default; if (checkBoxutf8.Checked) { en2 = Encoding.UTF8; } else if (checkBoxgbk.Checked) { en2 = Encoding.GetEncoding("GBK"); } else { en2 = Encoding.GetEncoding("GB2312"); } if (File.Exists(filename)) { try { string fstr = null; FileStream fs = new FileStream(filename, FileMode.Open); Encoding targetEncoding = GetType(fs); fs.Close(); using (StreamReader sr = new StreamReader(filename, targetEncoding)) { fstr = sr.ReadToEnd(); } int lastlen=filename.LastIndexOf("\");//结束的/ int startlen=filename.IndexOf("\");//开始的/ string temp = filename.Substring(lastlen+1); string tempqian = filename.Substring(startlen+1, (lastlen - startlen)-1); //Generate(tempqian);//生成一个文件夹 using (StreamWriter st = new StreamWriter(tempqian + "\" + temp, false, en2)) { st.Write(fstr); } Count[0] = Count[0]+1; LogBLL.Err("文件[" + filename + "]转换成功"); } catch (Exception e) { Count[1] = Count[1]+1; LogBLL.Err("文件[" + filename + "]转换失败[" + e .Message+ "]"); } } else { Count[2] = Count[2]+1; LogBLL.Err("文件[" + filename + "]不存在"); } } /// <summary> /// 创建文件夹 /// </summary> /// <param name="dirMu"></param> private void Generate(string dirMu) { if (!Directory.Exists(dirMu)) { Directory.CreateDirectory(dirMu); } else { //System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(dirMu); //foreach (System.IO.FileInfo f in path.GetFiles()) //{ // f.Delete(); //} } } /// <summary> /// 选择文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSelect_Click(object sender, EventArgs e) { this.butttrun.Enabled = false; this.buttonSelect.Enabled = false; FolderBrowserDialog FileDialogSelectFile = new FolderBrowserDialog(); DialogResult result = FileDialogSelectFile.ShowDialog(); SelectPath = FileDialogSelectFile.SelectedPath; if (result == DialogResult.OK && SelectPath != null) { List<string> names = GetFileName(SelectPath); if (names != null && names.Count > 0) { listViewFile.Clear();//清空列记录 ColumnHeader cZh = new ColumnHeader();//创建一个列 cZh.Text = "File Name"; cZh.Width = 433; //列名 listViewFile.Columns.AddRange(new ColumnHeader[] { cZh });//将这两列加入listView1 for (int i = 0; i < names.Count; i++) { ListViewItem lvi = new ListViewItem(new string[] { names[i] }, -1);//创建列表项 listViewFile.Items.Add(lvi);//将项加入listView1列表中 } MessageBox.Show(this, "总共发现" + names.Count+"相应的文件", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } else { SelectPath = null; MessageBox.Show(this, "没有找到相应的文件", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } } this.butttrun.Enabled = true; this.buttonSelect.Enabled = true ; } /// <summary> /// 获取一个路径下所有的文件 /// </summary> /// <param name="SelectedPath"></param> /// <returns></returns> public List<string> GetFileName(string SelectedPath) { List<string> Files = new List<string>(); string Filter = profile.ContentValue(confitemp, "Filter"); string[] files = Filter.Split('|'); foreach (var item in files) { if (item != null && item.StartsWith("*.")) { string[] fileName = Directory.GetFiles(SelectedPath, item, SearchOption.AllDirectories); Files.AddRange(fileName); } } return Files; } /// <summary> /// UTF-8编码格式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkBoxutf8_CheckedChanged(object sender, EventArgs e) { if (sender != null && sender.GetType()==typeof(CheckBox)) { CheckBox checkboxutf8=(CheckBox)sender; if (checkboxutf8.Checked) { this.checkBoxgb2312.Checked = false; this.checkBoxgbk.Checked = false; } } } /// <summary> /// GBK编码格式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkBoxgbk_CheckedChanged(object sender, EventArgs e) { if (sender != null && sender.GetType() == typeof(CheckBox)) { CheckBox checkboxgbk = (CheckBox)sender; if (checkboxgbk.Checked) { this.checkBoxgb2312.Checked = false; this.checkBoxutf8.Checked = false; } } } /// <summary> /// GB2312编码格式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkBoxgb2312_CheckedChanged(object sender, EventArgs e) { if (sender != null && sender.GetType() == typeof(CheckBox)) { CheckBox checkboxutf8 = (CheckBox)sender; if (checkboxutf8.Checked) { this.checkBoxutf8.Checked = false; this.checkBoxgbk.Checked = false; } } } /// <summary> /// 加载次程序的时候启动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FileTranscoding_Load(object sender, EventArgs e) { if (File.Exists(profile.StartUpIniFileName)) {} else { profile.xieru(); } } }}
?转载请注明http://yuyu456.iteye.com/admin/blogs/1721470