Kymo

My .Net blog
随笔 - 4, 文章 - 0, 评论 - 11, 引用 - 0
数据加载中……

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

本人工作环境为繁体,今天从园子里找了一些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 on 2008-04-22 23:43 KymoWang 阅读(450) 评论(8)  编辑 收藏 所属分类: Tools

评论

#1楼    回复  引用  查看    

我有一个开源的比你这个功能强很多
2008-04-23 08:39 | 元宝      

#2楼    回复  引用  查看    

@元宝
把链接贴出来啊~~
2008-04-23 11:52 | lexus      

#3楼    回复  引用  查看    

用iconv命令,一下搞定!!
2008-04-23 19:36 | 天下叁      

#4楼 [楼主]   回复  引用  查看    

@天下叁
学习了
2008-04-23 21:45 | KymoWang      

#5楼    回复  引用  查看    

@lexus
Baidu一下就可以啦,這里有
http://down.chinaz.com/soft/21734.htm
2008-04-24 14:13 | 凯锐      

#6楼    回复  引用  查看    

--引用--------------------------------------------------
KymoWang: @天下叁
学习了

--------------------------------------------------------
2008-04-25 04:36 | 镜涛      

#7楼    回复  引用    

尊敬的开发者你好,我是www.svnhost.cn站长小灰, 诚挚的邀请你加入svnhost大家庭,这里可以免费托管你的代码,使用SVN做源代码版本管理。

#8楼    回复  引用  查看    

凯锐说得对,搜一下就有了,网上很多文件编码转换器源码 v1.0都转自我的
http://www.cnblogs.com/yuanbao/archive/2008/01/30/1059065.html
2008-05-04 14:39 | 元宝      

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-04-22 23:45 编辑过
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: