转换源文件编码(博客处女作)

本人工作环境为繁体,今天从园子里找了一些SourceCode,结果中文注释在VS里全变成了乱码,源文件又比较多,就写了一个小工具,顺便学习一下编码的转换。
功能:把给定目录里面的cs文件全部由gb2312转换成unicoe,并备份源文件。
考虑可能不是很周到,两个知识点:编码转换和目录遍历

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConvertEndcode
{
    
/// <summary>        
    
/// Author: Kymo Wang
    
/// Date: 2008/04/21
    
/// Description: Convert file from source encoding to destination encoding
    
/// </summary>

    
    
public class ConvertEncoder
    
{
        
private Encoding encFrom = Encoding.GetEncoding("GB2312");
        
private Encoding encTo = Encoding.Unicode;
        
private string filePath;

        
public ConvertEncoder(string filePath)
        
{
            
this.filePath = filePath;
            Convert();
        }


        
private void Convert()
        
{
            StreamReader sr 
= null;
            StreamWriter sw 
= null;
            
try
            
{
                
//Backup the original file
                if (File.Exists(filePath + ".bak"))
                
{
                    Console.WriteLine(
"File {0} already exists!", filePath + ".bak");
                    
return;
                }

                    
                File.Copy(filePath, filePath 
+ ".bak");

                
//Read file
                sr = new StreamReader(filePath, encFrom);
                
string strContent = sr.ReadToEnd();
                sr.Close();
                
byte[] byteSource = encFrom.GetBytes(strContent);

                
//Convert source content to Unicode
                byte[] byteDest = Encoding.Convert(encFrom, encTo, byteSource);
                
char[] charDest = encTo.GetChars(byteDest);
                strContent 
= new string(charDest);

                
//Write content with Unicode
                File.Delete(filePath);
                FileStream stream 
= new FileStream(filePath, FileMode.OpenOrCreate);
                sw 
= new StreamWriter(stream, encTo);
                sw.Write(strContent);
                Console.WriteLine(
"Converting file: {0}", filePath);
            }

            
catch
            
{
                
throw;
            }

            
finally
            
{
                
if (sr != null)
                
{
                    sr.Close();
                }

                
if (sw != null)
                
{
                    sw.Close();
                }

            }

        }

    }

}



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConvertEndcode
{
    
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
if (args.Length < 1)
            
{
                Console.WriteLine(
"Please input directory!");
                
return;
            }

            
new Program().TraverseDir(args[0]);
        }


        
//Traverse direcotry
        private void TraverseDir(string dir)
        
{
            
if (!Directory.Exists(dir))
            
{
                Console.WriteLine(
"Dir [{0}] not exists!", dir);
                
return;
            }

            
string[] fileList = Directory.GetFiles(dir);
            
foreach (string fileName in fileList)
            
{
                
if (fileName.EndsWith(".cs", StringComparison.CurrentCulture))
                
{
                    ConvertEncoder convert 
= new ConvertEncoder(fileName);
                }

            }

            
string[] dirList = Directory.GetDirectories(dir);
            
foreach (string dirName in dirList)
            
{
                TraverseDir(dirName);
            }

        }

            
    }

}


posted @ 2008-04-22 23:43  KymoWang  阅读(981)  评论(8编辑  收藏  举报