当前位置:编程学习 > C#/ASP.NET >>

ASP.NET静态页面的绝对路径转相对路径

经常有新手在做页面的时候不注意路径的安排,通常图方便都写成绝对路径。结果在编辑的时候(非dreamware下)发现不能直接点开,非要经过服务器才可以看到效果。

于是写了一段代码,直接将这些有规律的绝对链接转为相对链接,代码如下:

程序代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace FilePathTool
{
   class FilePath
   {
     //文件的根目录
     static readonly string RootLocal = @"F:workfolderproject";
     List<FileItem> files = new List<FileItem>();
     public FilePath()
     {
       DirectoryInfo root = new DirectoryInfo(RootLocal);
       GetFolders(root, 0,"");
       DealFile();
       Console.WriteLine(files.Count);
       Console.Read();
     }
     /// <summary>
     /// 得到文件夹
     /// </summary>
     void GetFolders(DirectoryInfo root, int Level, string path)
     {
       foreach (DirectoryInfo item in root.GetDirectories())
       {
         GetFolders(item, Level + 1, path + item.Name + "/");
       }
       //得到文件
       foreach (FileInfo item in root.GetFiles())
       {
         files.Add(new FileItem {
           Level = Level,
           Name = item.Name,
           Extension = item.Extension,
           Path = item.FullName,
           AbsolutePath = path
         });
       }
     }
     /// <summary>
     /// 处理已得到的文件
     /// </summary>
     void DealFile()
     {
       foreach (FileItem item in files)
       {
         if (item.Extension == ".htm")
         {
           Console.WriteLine(item.AbsolutePath);
           DealRepalce(item);
         }
       }
     }
     /// <summary>
     /// 替换方法
     /// </summary>
     void DealRepalce(FileItem item)
     {
       FileInfo file = new FileInfo(item.Path);
       string txt = File.ReadAllText(item.Path, Encoding.Default);
       txt = RepalceProcess(txt, item);
       Console.WriteLine(txt);
       //Console.Read();
       File.WriteAllText(item.Path, txt, Encoding.Default);
     }
     /// <summary>
     /// 遍历替换
     /// </summary>
     string RepalceProcess(string txt, FileItem model)
     {
       foreach (FileItem item in files)
       {
         string relativePath = item.AbsolutePath + item.Name;
         if (txt.Contains(relativePath))
         {
           txt = RepalceSingle(txt, relativePath, item, model);
         }
       }
       return txt;
     }
     /// <summary>
     /// 替换
     /// </summary>
     string RepalceSingle(string txt, string relativePath, FileItem item, FileItem model)
     {
       //当文件在同一目录下只不需要显示路径
       if (item.AbsolutePath == model.AbsolutePath)
       {
         return txt.Replace("/" + relativePath, item.Name);
       }
       else
       {
         return txt.Replace("/" + relativePath, GetDeep(model.Level) + relativePath);
       }
     }
     static string tempDeep = @"../";
     /// <summary>
     /// 按文件深度得到父路径
     /// </summary>
     /// <param name="Level"></param>
     /// <returns></returns>
     string GetDeep(int Level)
     {
       string deep = "";
       for (int i = 0; i < Level; i++)
       {
         deep += tempDeep;
       }
       return deep;
     }
   }
   /// <summary>
   /// 文件信息实体
   /// </summary>
   public class FileItem
   {
     public int Level
     {
       get;
       set;
     }
     public string Name
     {
       get;
       set;
     }
     public string Extension
     {
       get;
       set;
     }
     public string Path
     {
       get;
       set;
     }
     public string AbsolutePath
     {
       get;
       set;
     }
   }
}


www.zzzyk.com 每天都为您更新最新的电脑知识信息

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,