开发者

C# or VB.NET: How to programatically change encoding format to UTF-8 of all text files in a given directory?

I am trying to loop through a directory and 开发者_如何学运维for ever text file that I find, convert the encoding to UTF-8 Fromat.


Use DirectoryInfo and you're pretty much done

DirectoryInfo DI = new DirectoryInfo("TextFiles_Path");
FileInfo[] Files = DI.GetFiles("*.txt");
foreach(FileInfo Fl in Files)
{
    StreamReader SR = Fl.OpenText(); //This opens a stream to the file **in UTF8 encoding**
    StreamWriter SW = new StreamWriter(new FileStream(Fl.FullName + ".UTF8.txt", FileMode.OpenOrCreate), Encoding.UTF8);
    SW.Write(SR.ReadToEnd());
}

Enjoy


Fast and easy

For Each oFile In IO.Directory.GetFiles(dir, "*.*", IO.SearchOption.AllDirectories)
    IO.File.WriteAllText(oFile, IO.File.ReadAllText(oFile), Encoding.UTF8)
Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜