jekeyhui99 发表于 2019-1-7 22:24:37

c#替换文件目录及其子目录中的文件内容

<p>using System;</p><p>using System.Collections.Generic;</p><p>using System.IO;</p><p>using System.Linq;</p><p>using System.Text;</p><p>using System.Threading.Tasks;</p><p><br></p><p>namespace urlChange</p><p>{</p><p>&nbsp; &nbsp; class Program</p><p>&nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("替换该文件及其子文件夹下*html文档内容。");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("请输入文件目录:");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string s = null;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;DirectoryInfo&gt; fileList = new List&lt;DirectoryInfo&gt;();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List&lt;FileInfo&gt; htmlList=new List&lt;FileInfo&gt;();</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //输入文件url</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = Console.ReadLine();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //创建文件目录</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DirectoryInfo dir = new DirectoryInfo(@s);</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //把文件目录信息存到集合中</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileList.Add(dir);</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得文件目录中的子目录</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DirectoryInfo[] dii = dir.GetDirectories();</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetFileUrl1(fileList,dii);</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //此时,fileList存有所有目录(包含子目录)的信息</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("共有"+fileList.Count+"个文件夹\n");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //循环去除每个目录的信息</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (DirectoryInfo dif in fileList)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得每个目录的所有文件</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileInfo[] fils = dif.GetFiles();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //循环取得每个文件</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (FileInfo fil in fils)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //后缀名为html的文件</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(fil.FullName.Contains((".html")))</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得文件内容</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReplaceText(fil.FullName.ToString());</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //把每个文件的信息存入集合</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; htmlList.Add(fil);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //输出文件目录与显示总共有多少个文件</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(fil.FullName+"---"+ ++i);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("该文件夹及其所有子文件夹下共有"+htmlList.Count+"个文件\n");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("替换成功!");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p><br></p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; public static void GetFileUrl1(List&lt;DirectoryInfo&gt; fileList, DirectoryInfo[] dir)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得子目录中的子目录</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (DirectoryInfo dif in dir)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //把子目路信息存到集合</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileList.Add(dif);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得子目录中的子目录</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DirectoryInfo[] dii1 = dif.GetDirectories();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //要是子目录中还有目录,则取得子目录中的子目录</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dii1.Length&gt;0)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetFileUrl1(fileList,dii1);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; public static void ReplaceText(string s)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string path = s;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string con = "";</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得文件的内容</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"),true);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con = sr.ReadToEnd();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //替换内容</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con = con.Replace("http:","");</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sr.Close();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.Close();</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取得文件的内容</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileStream fs2 = new FileStream(path,FileMode.Open,FileAccess.Write);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamWriter sw=new StreamWriter(fs2, Encoding.UTF8);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //把替换后的文本内容存到文本中</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sw.WriteLine(con);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sw.Close();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs2.Close();</p><p><br></p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; }</p><p>}</p><p>---------------------&nbsp;</p><p>作者:清--水&nbsp;</p><p>来源:CSDN&nbsp;</p><p>原文:https://blog.csdn.net/yh12346789/article/details/80146687&nbsp;</p><p></p>
页: [1]
查看完整版本: c#替换文件目录及其子目录中的文件内容