Saturday, July 09, 2011

C# Batch Replace with encoding

Here is the code I wrote for batch replace with GB2312 encoding:-

var dir = new DirectoryInfo(@"D:\hgb");
string strFileName;
string content;
var encoding = Encoding.GetEncoding(936);

foreach (var f in dir.GetFiles("*.htm", SearchOption.AllDirectories))
{
strFileName = f.DirectoryName + @"\" + f.Name;
using (StreamReader reader = new StreamReader(strFileName, encoding))
{
content = reader.ReadToEnd();
content = Regex.Replace(content, "font size=-1", "font size=48");
}




StreamWriter writer = new StreamWriter(strFileName, false, encoding);
writer.Write(content);
writer.Close();
}

2 comments:

Mark Ortega said...

As I am learning C#, this would be a great help to me to gain further knowledge to C#.

Web Mavens said...

Such a very nice coding. Nice post.

Offshore Software Development Centre

Web Mavens provides experienced dedicated web developers to all kinds of businesses from startups to large enterprises. We’ve successfully delivered over 80 projects and our team of web developers is proud to have always been a valuable contribution to our clients’ success.