修改文件夹下所有文件并保存

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Text.RegularExpressions;
namespace 更改文件名
{
    class Program
    {
         
       static void Main(string[] args)
        {
            string path = @"D:\Web";
            DirectoryInfo TheFolder = new DirectoryInfo(path);
            string txt="";
            foreach (FileInfo item in TheFolder.GetFiles())
            {
                string name = item.Name;
                string[] houzhui = name.Split('.');
                string readtxt;
                bool pp = false;
                if (houzhui.Count()==2)
                {
                    using (StreamReader sr=new StreamReader(path+"\\"+item.Name,Encoding.UTF8))
                    {
                        readtxt = sr.ReadToEnd();
                        string pattern = @"inherits=\W\S*, \w*\W";
                        string inherits;
                       
                        foreach (Match match in Regex.Matches(readtxt, pattern))
                        {
                            pp = true;
                            string inher = match.Value.Substring(10,match.Value.LastIndexOf('"')-10);
                            inherits = "Inherits=" + '"' + inher.Split(',')[0] + '"' + " CodeBehind="+'"'+item.Name+".cs"+'"';
                          txt= readtxt.Replace(match.Value,inherits);
                        }
                      
                    }
                    if (pp)
                    {
                        using (StreamWriter sw = new StreamWriter(path + "\\" + item.Name))
                        {
                            sw.Write(txt);
                        }
                    }
                    
                }
                else
                {
                     
                }
            }
        }
    }
}

当前名称:修改文件夹下所有文件并保存
转载来源:http://bzwzjz.com/article/jhisec.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 响应式网站设计方案 成都网站建设 成都网站建设推广 H5网站制作 企业网站设计 成都响应式网站建设 网站建设改版 网站建设公司 移动手机网站制作 成都网站制作 成都网站设计制作公司 成都网站建设 成都网站设计 高端网站设计 品牌网站建设 成都网站建设流程 专业网站设计 网站制作 自适应网站建设 成都商城网站建设 成都网站建设 成都网站制作