使用C#编写的文件批量替换器

来源:互联网 发布:web前端数据库 编辑:程序博客网 时间:2024/05/22 13:35

在使用ASP.NET写项目中aspx文件和文档声明对于浏览器兼容性有很大影,我手头就有一个项目原来是在IE6下跑的没有问题,

可是换成IE8后就有很多问题,搞了半天发现只要将文档的头声明更改一下就可以了,将原来的声明如下:

Code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >  

换成如下,

Code:
  1. <!DOCTYPE html>  
  2. <html>  

样式的大部分问题就可以搞定,

换吧使用Ctrl+H在VS2008一跑全部项目替换,结果死了,文件太多600多个aspx文件,无奈自己写了一个小工具来完成这个功能,

感觉挺实用的就放出来!

老鸟路过!

界面:

代码:

Code:
  1. private void button1_Click(object sender, EventArgs e)   
  2.       {   
  3.           if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)   
  4.           {   
  5.               txtpath.Text = folderBrowserDialog1.SelectedPath;   
  6.           }   
  7.       }   
  8.   
  9.       private void button2_Click(object sender, EventArgs e)   
  10.       {   
  11.           //验证   
  12.           errorProvider1.SetError(txtpath, "");   
  13.           if (txtpath.Text.Length == 0)   
  14.           {   
  15.               errorProvider1.SetError(txtpath, "不为空!");   
  16.               txtpath.Focus();   
  17.               return;   
  18.           }   
  19.   
  20.           errorProvider1.SetError(txtExt, "");   
  21.           if (txtpath.Text.Length == 0)   
  22.           {   
  23.               errorProvider1.SetError(txtExt, "不为空!");   
  24.               txtExt.Focus();   
  25.               return;   
  26.           }   
  27.   
  28.           n = 0;   
  29.           //处理文件   
  30.           GoProcFile(txtpath.Text);   
  31.           this.Text = "完成!共处理"+n+"个文件!";   
  32.           n = 0;   
  33.   
  34.       }   
  35.       int n = 0;   
  36.       void GoProcFile(string path)   
  37.       {   
  38.           foreach (var f in Directory.GetFiles(path, txtExt.Text))   
  39.           {   
  40.               n++;   
  41.               //替换文件   
  42.               ReplaceFile(f);   
  43.           }   
  44.           //如果处理子文件夹   
  45.           if (checkBox1.Checked)   
  46.           {   
  47.               foreach (var dir in Directory.GetDirectories(path))   
  48.               {   
  49.                   GoProcFile(dir);   
  50.               }   
  51.   
  52.           }   
  53.       }   
  54.   
  55.       private void ReplaceFile(string f)   
  56.       {   
  57.           StreamReader sr = new StreamReader(f, Encoding.Default);   
  58.           StringBuilder sb = new StringBuilder(sr.ReadToEnd());   
  59.           sr.Close();   
  60.           sb = sb.Replace(txt1.Text, txt2.Text);   
  61.           StreamWriter sw = new StreamWriter(f, false, Encoding.Default);   
  62.           sw.Write(sb.ToString());   
  63.           sw.Close();   
  64.           this.Text = f;   
  65.           Thread.Sleep(1);   
  66.       }  

程序及源代码已放在我的下载下了:http://lijun7788.download.csdn.net/