昨天下午自己琢磨出来的:-
- protected void Page_Load(object sender, EventArgs e)
- {
- Bind(@"C:\Users\Only\Desktop\css\");//路径请替换成自己的
- }
- /// <summary>
- /// 去掉css文件中的空格和换行(压缩文件)
- /// </summary>
- /// <param name="path">物理路径</param>
- public void Bind(string path)
- {
- Response.Write(path + "<br/>");//输出页面信息
- DirectoryInfo dirInfo = new DirectoryInfo(path);//实例化
- //1.循环文件
- foreach (FileInfo file in dirInfo.GetFiles("*.css")) //循环扩展名为*.css的文件
- {
- string outpath = path + "\\" + file.Name;//整合文件路径
- string Content = File.ReadAllText(outpath).Replace("\r\n", "").Replace(" ", "");//去除空格换行
- File.WriteAllText(outpath, Content);//写入文件
- Response.Write(outpath+"<br/>");//输出页面信息
- }
- //2.循环文件夹
- foreach (DirectoryInfo folder in dirInfo.GetDirectories())
- {
- Bind(path + "\\" + folder.Name); //递归遍历其它文件夹
- }
-
- }
复制代码 有什么好的见解? |