中房网站由于论坛系统的变更,经历了两次整站编码的变更,开始采用cs的时候,不得不使用了UTF-8编码,这样一来,所有的aspx,ascx都得utf-8保存了,后来换成了discuz,就又要改回gb2312,页面多啊,手动改不完,么办?
上网找到了一些程序,不过都不顺手,于是就自己写一个吧!不知不觉搞了一整天,这不出来了,还挺好用,看看截图:

怎么样,看起来不错吧,呵呵。下面给出主要源代码:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.IO;
using
System.Text.RegularExpressions;
using
System.Threading;

namespace
CodeConvert

...
{
public partial class Form1 : Form

...{
string sourceFolder = string.Empty;
string targetCode = string.Empty;
string sourceCode = string.Empty;
string fileType = string.Empty;
string FolderPath = string.Empty;
Thread thread;

public Form1()

...{
InitializeComponent();
timerStatus.Enabled = true;
}

private void btnBrowse_Click(object sender, EventArgs e)

...{
fbdSourceFolder.ShowDialog();
if (fbdSourceFolder.SelectedPath != null && fbdSourceFolder.SelectedPath != "")

...{
tbSourceFolder.Text = fbdSourceFolder.SelectedPath;
}
}

private void btnStart_Click(object sender, EventArgs e)

...{
sourceFolder = tbSourceFolder.Text;
targetCode = cbTargetCode.Text;
sourceCode = cbSourceCode.Text;
fileType = tbFileType.Text.ToLower();

if (sourceFolder==string.Empty)

...{
MessageBox.Show("请指定源文件目录!", "错误");
return;
}

lvResult.Items.Clear();

FolderPath = sourceFolder;

thread = new Thread(new ThreadStart(this.Convert));
thread.Start();
btnStart.Enabled = false;
}

private void Convert()

...{
string[] fileTypesArray = fileType.Split(';');

DirectoryInfo di = new DirectoryInfo(FolderPath);
if (!di.Exists)

...{
MessageBox.Show("您指定的目录不存在!","错误");
return;
}

FileInfo[] subFiles = di.GetFiles();
DirectoryInfo[] subFolders = di.GetDirectories();

//处理每个文件
foreach (FileInfo file in subFiles)

...{
for (int i = 0; i < fileTypesArray.Length; i++)

...{
string partten = string.Format("({0})$", fileTypesArray[i].Replace(".", "/."));
partten = partten.Replace("*", @"[^/;?*'<>]");
Regex rg = new Regex(partten);
if (rg.IsMatch(file.FullName.ToLower()))

...{
ReCode(file);
}
}
}

//递归处理每个目录
foreach (DirectoryInfo dir in subFolders)

...{
FolderPath = dir.FullName;
Convert();
}
}

private void ReCode(FileInfo file)

...{
lblNowConverting.Text = file.FullName;
//去掉文件的只读属性和隐藏
bool isModified = false;
FileAttributes fa = File.GetAttributes(file.FullName);
if (fa.ToString().IndexOf("ReadOnly") >= 0)

...{
isModified = true;
File.SetAttributes(file.FullName,FileAttributes.Archive);
}

Encoding sourceEncoding;
if (sourceCode.Equals("自动判断"))

...{
try

...{
sourceEncoding = TxtFileEncoding.GetEncoding(file.FullName, Encoding.GetEncoding(cbDefaultCode.Text));
}
catch

...{
try

...{
sourceEncoding = Encoding.GetEncoding(cbDefaultCode.Text);
}
catch

...{
thread.Abort();
MessageBox.Show("无效的默认编码!", "错误");
return;
}
}
}
else

...{
try

...{
sourceEncoding = Encoding.GetEncoding(sourceCode);
}
catch

...{
thread.Abort();
MessageBox.Show("无效的源编码!", "错误");
return;
}
}

StreamReader SRead = new StreamReader(file.FullName,sourceEncoding,false);
string sourceText = string.Empty;
sourceText = SRead.ReadToEnd();
SRead.Close();

//备份原文件
if (cbBackup.Checked)

...{
if (File.Exists(file.FullName + ".bak"))

...{
File.SetAttributes(file.FullName + ".bak", FileAttributes.Archive);
}
file.CopyTo(file.FullName + ".bak",true);
}

Encoding targetEncoding;
try

...{
targetEncoding = Encoding.GetEncoding(targetCode);
}
catch

...{
thread.Abort();
MessageBox.Show("无效的目标编码!", "错误");
return;
}


//以新编码写文件
StreamWriter SW = new StreamWriter(file.FullName, false, targetEncoding);
SW.Write(sourceText);
SW.Close();

//恢复属性
if (isModified)

...{
File.SetAttributes(file.FullName, fa);
}


ListViewItem lviItem = new ListViewItem();
ListViewItem.ListViewSubItem lvsFile = new ListViewItem.ListViewSubItem(lviItem,file.FullName);
ListViewItem.ListViewSubItem lvsSource = new ListViewItem.ListViewSubItem(lviItem, sourceEncoding.EncodingName);
ListViewItem.ListViewSubItem lvsTarget = new ListViewItem.ListViewSubItem(lviItem, targetCode);
lviItem.SubItems.Insert(0,lvsFile);
lviItem.SubItems.Insert(1,lvsSource);
lviItem.SubItems.Insert(2,lvsTarget);
lvResult.Items.Insert(0,lviItem);

lblNowConverting.Text = string.Empty;

}

private void lvResult_SizeChanged(object sender, EventArgs e)

...{
lvResult.Height = gbResult.Height - 60;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

...{
if (thread != null)

...{
thread.Abort();
}
}

private void btnStop_Click(object sender, EventArgs e)

...{
if (thread != null)

...{
thread.Abort();
}
lblNowConverting.Text = string.Empty;
}

private void btnHelp_Click(object sender, EventArgs e)

...{
MessageBox.Show(@"1. 程序将自动搜索目标目录下所有的指定类型的文本型文件,并备份原始文件。
2. 请勿指定非文本类型的文件进行转换,否则将损坏源文件。
3. 默认编码类型是由于GB2312等无法从文本判断编码而设置。", "使用说明");
}

private void timerStatus_Tick(object sender, EventArgs e)

...{
if (thread != null)

...{
switch (thread.ThreadState)

...{
case ThreadState.Running:
labelNow.Text = "正在转换";
btnStop.Enabled = true;
break;
case ThreadState.Stopped:
labelNow.Text = "准备就绪";
btnStop.Enabled = false;
btnStart.Enabled = true;
break;
case ThreadState.Aborted:
labelNow.Text = "已经停止";
btnStart.Enabled = true;
btnStop.Enabled = false;
break;
default:
labelNow.Text="准备就绪";
btnStart.Enabled = true;
btnStop.Enabled = false;
break;
}
}
}
}
}
欢迎指教!下面是项目文件和所有源代码,有需要的朋友们下载吧!
点这里下载