Last active
August 15, 2022 08:08
-
-
Save yohey03518/3fd984a6cfe0c92c7482cbb4584e94dd to your computer and use it in GitHub Desktop.
判斷某路徑底下Big5編碼類型檔案轉成UTF8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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