`
lonestone
  • 浏览: 92862 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

文本编码批量转换器——搞asp.net必备

阅读更多

中房网站由于论坛系统的变更,经历了两次整站编码的变更,开始采用cs的时候,不得不使用了UTF-8编码,这样一来,所有的aspx,ascx都得utf-8保存了,后来换成了discuz,就又要改回gb2312,页面多啊,手动改不完,么办?

上网找到了一些程序,不过都不顺手,于是就自己写一个吧!不知不觉搞了一整天,这不出来了,还挺好用,看看截图:

怎么样,看起来不错吧,呵呵。下面给出主要源代码:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
usingSystem.Text.RegularExpressions;
usingSystem.Threading;

namespaceCodeConvert
...{
publicpartialclassForm1:Form
...{
stringsourceFolder=string.Empty;
stringtargetCode=string.Empty;
stringsourceCode=string.Empty;
stringfileType=string.Empty;
stringFolderPath=string.Empty;
Threadthread;

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


privatevoidbtnBrowse_Click(objectsender,EventArgse)
...{
fbdSourceFolder.ShowDialog();
if(fbdSourceFolder.SelectedPath!=null&&fbdSourceFolder.SelectedPath!="")
...{
tbSourceFolder.Text
=fbdSourceFolder.SelectedPath;
}

}


privatevoidbtnStart_Click(objectsender,EventArgse)
...{
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
=newThread(newThreadStart(this.Convert));
thread.Start();
btnStart.Enabled
=false;
}


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

DirectoryInfodi
=newDirectoryInfo(FolderPath);
if(!di.Exists)
...{
MessageBox.Show(
"您指定的目录不存在!","错误");
return;
}


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

//处理每个文件
foreach(FileInfofileinsubFiles)
...{
for(inti=0;i<fileTypesArray.Length;i++)
...{
stringpartten=string.Format("({0})$",fileTypesArray[i].Replace(".","\."));
partten
=partten.Replace("*",@"[^/;?*'<>]");
Regexrg
=newRegex(partten);
if(rg.IsMatch(file.FullName.ToLower()))
...{
ReCode(file);
}

}

}


//递归处理每个目录
foreach(DirectoryInfodirinsubFolders)
...{
FolderPath
=dir.FullName;
Convert();
}

}


privatevoidReCode(FileInfofile)
...{
lblNowConverting.Text
=file.FullName;
//去掉文件的只读属性和隐藏
boolisModified=false;
FileAttributesfa
=File.GetAttributes(file.FullName);
if(fa.ToString().IndexOf("ReadOnly")>=0)
...{
isModified
=true;
File.SetAttributes(file.FullName,FileAttributes.Archive);
}


EncodingsourceEncoding;
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;
}

}


StreamReaderSRead
=newStreamReader(file.FullName,sourceEncoding,false);
stringsourceText=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);
}


EncodingtargetEncoding;
try
...{
targetEncoding
=Encoding.GetEncoding(targetCode);
}

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



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

//恢复属性
if(isModified)
...{
File.SetAttributes(file.FullName,fa);
}



ListViewItemlviItem
=newListViewItem();
ListViewItem.ListViewSubItemlvsFile
=newListViewItem.ListViewSubItem(lviItem,file.FullName);
ListViewItem.ListViewSubItemlvsSource
=newListViewItem.ListViewSubItem(lviItem,sourceEncoding.EncodingName);
ListViewItem.ListViewSubItemlvsTarget
=newListViewItem.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;

}


privatevoidlvResult_SizeChanged(objectsender,EventArgse)
...{
lvResult.Height
=gbResult.Height-60;
}


privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse)
...{
if(thread!=null)
...{
thread.Abort();
}

}


privatevoidbtnStop_Click(objectsender,EventArgse)
...{
if(thread!=null)
...{
thread.Abort();
}

lblNowConverting.Text
=string.Empty;
}


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


privatevoidtimerSta
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics