Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
判斷某路徑底下Big5編碼類型檔案轉成UTF8
static void Main(string[] args)
{
// 取得要搜尋的根目錄
string path = ConfigurationManager.AppSettings["directory"];
var files = new DirectoryInfo(path).GetFiles("*.cs", SearchOption.AllDirectories);
foreach (var file in files)
{
if (file.Name.StartsWith("Tempo"))
{
Console.WriteLine("暫存檔不處理! ");
continue;
}
// 檢查檔案是否為Big5,若是則以big5編碼將檔案內容讀入後,再以UTF-8回存
if (IsBig5Encoding(file.FullName))
{
Console.WriteLine(string.Format("發現big5檔案:{0} ", file.FullName));
Console.Write("開始轉換.. ");
string fileContent = File.ReadAllText(file.FullName, Encoding.GetEncoding("big5"));
File.WriteAllText(file.FullName, fileContent, Encoding.UTF8);
Console.WriteLine(" 轉換完畢! ");
}
}
Console.WriteLine();
Console.WriteLine("執行完畢!");
Console.ReadKey();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment