首 页文章中心下载中心动画频道客户留言泉州论坛资讯中心游戏中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:今日下载站 -> ASP/PHP/.NET/XML -> 文章内容-> c#初学乍练-文本替换工具命令行版 退出登录 用户管理
栏目导航
· 办公自动化 · 软件工程
· Java/J2EE/JSP · VC++/C#/C
· Visual Basic · Delphi/Perl
· ASP/PHP/.NET/XML · PowerBuilder
· SQL Server/My SQL · Oracle
· Access · DB2/FOXPRO
· Windows · Linux
· Unix · CorelDRAW
· AutoCAD/Firework · Photoshop
· Dreamweaver · Flash/3DS Max
· 网页特效 · 安全防护
热门文章
相关文章
· [图文] 超炫效..
· [图文] 实用教..
· [图文] 独家视..
· [图文] 用历史..
· [图文] Photo..
· [图文] 用两个..
· [图文] photo..
· [图文] 菜鸟学..
· [图文] Photo..
· [图文] 菜鸟扣..

c#初学乍练-文本替换工具命令行版

作者:佚名  来源:本站整理  发布时间:2006-12-22 12:58:46  发布人:今日下载站

减小字体 增大字体

该程序使用正则表达式进行文字替换,广度优先遍历子目录(基础知识很重要), 解决无法替换回车换行的问题
 class Replacee
 {
   
  /// <summary>
  /// 替换文件中字符
  /// </summary>
  /// <param name="fileFullName">文件全名</param>
  /// <param name="replacedBy">用于替换的字符串</param>
  /// <param name="findPattern">用于查找的字符串</param>
  /// <param name="isBackup">是否备份文件</param>
  private static void DoReplace(string fileFullName, string replacedBy, string findPattern, bool isBackup)
  {
   string result = string.Empty;
   string inputText = string.Empty;
   string replacement = replacedBy;
   string pat = findPattern;
   Regex r = new Regex(pat, RegexOptions.IgnoreCase);
  
   try
   {
    using (StreamReader sr = new StreamReader(fileFullName))
    {
     inputText = sr.ReadToEnd();
    }
     
    // Compile the regular expression.
    if (r.IsMatch(inputText))
    {
     if (isBackup == true)
     {
      try
      {
       File.Copy(fileFullName, fileFullName + ".bak");
      }
      catch(System.IO.IOException ex)
      {
       File.Copy(fileFullName, fileFullName + ".bak", true);
       Console.WriteLine(ex.Message);
      }
     }
     result = r.Replace(inputText, replacement);               
          
     // Add some text to the file.
     using (StreamWriter sw = new StreamWriter(fileFullName))
     {
      sw.Write(result);
     }
    }            
    Console.WriteLine(fileFullName);
   }
   catch (Exception e)
   {
           
    Console.WriteLine("The process failed: {0}", e.ToString());          
    //throw(e);
   }
  }
   
  /// <summary>
  /// 遍历目录
  /// </summary>
  /// <param name="path">起始路径</param>
  /// <param name="replacedBy">用于替换的字符串</param>
  /// <param name="findPattern">用于查找的字符串</param>
  /// <param name="isBackup">是否备份文件</param>
  /// <param name="isGetSubFloder">是否获取子文件夹</param>
  public static void TravelReplace(string path, string replacedStr, string findPattern, bool isBackup, bool isGetSubFloder)
  {
   Queue queue = new Queue();
   DirectoryInfo di = null;
   string subPath = string.Empty;
   string currentPath = string.Empty;
   FileSystemInfo[] dirs = null;

   queue.Enqueue(path);
   while (queue.Count > 0)
   {    
    currentPath = (string)queue.Dequeue();
    di = new DirectoryInfo(currentPath);
    
    //get files under current directiory
    FileSystemInfo[] files = di.GetFiles("*.sql");
    foreach (FileSystemInfo f in files)
    {
     DoReplace(f.FullName, replacedStr, findPattern, isBackup);    
    }

    // Get only subdirectories
    if (isGetSubFloder == true)
    {     
     dirs = di.GetDirectories();
     foreach (FileSystemInfo d in dirs)
     {
      subPath = d.FullName;
      queue.Enqueue(subPath);
      Console.WriteLine(subPath);      
     }
    }
   }
  }
 }
 
测试:
    Replacee.TravelReplace(@"E:\temp\ttt", "\r\n);", @"(\r\n){2,}\);", true, true);


本文地址:http://www.newbooks.com.cn/info/23328.html
[] [返回上一页] [打 印] [收 藏]
上一篇文章: faq:扩展chtmlview
下一篇文章: 系统出错信息设计
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 管理登录
Copyright © 2002-2006 cntoday.com.cn. All Rights Reserved .
Powered by:NewAsp SiteManageSystem Version 2.1 SP1 1030