Asp. NET static page absolute path to relative path

Source: Internet
Author: User
Tags foreach relative root directory

Often a novice in the page when do not pay attention to the path of the arrangement, usually the diagram is convenient to write an absolute path. Results at the time of editing (not dreamware) found that can not be directly opened, not to go through the server to see the effect.

So write a piece of code, directly to these regular absolute links into relative links, the code is as follows:

Program code

using System;


using System.Collections.Generic;


using System.IO;


using System.Text;


namespace Filepathtool


{


class FilePath


   {


//File root directory


static readonly string rootlocal = @ "F:\workfolder\project\";


list<fileitem> files = new list<fileitem> ();


public FilePath ()


     {


DirectoryInfo root = new DirectoryInfo (rootlocal);


getfolders (root, 0, "");


Dealfile ();


Console.WriteLine (Files. Count);


Console.read ();


     }


///<summary>


///Get folder


///</summary>


void Getfolders (directoryinfo root, int level, string path)


     {


foreach (DirectoryInfo item in root. GetDirectories ())


       {


getfolders (item, level + 1, path + Item). Name + "/");


       }


//Get file


foreach (FileInfo item in root. GetFiles ())


       {


files. ADD (new Fileitem {


level = level,


Name = Item. Name,


Extension = Item. Extension,


Path = Item. FullName,


Absolutepath = Path


         });


       }


     }


///<summary>


///Process The resulting file


///</summary>


void Dealfile ()


{


foreach (Fileitem item in files)


       {


if (item. Extension = = ". htm")


         {


Console.WriteLine (item. Absolutepath);


Dealrepalce (item);


         }


       }


     }


///<summary>


///Replacement Method


///</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>


Traversal substitution


///</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>


///Replace


///</summary>


string Repalcesingle (String txt, string relativepath, Fileitem item, Fileitem model)


     {


///When the file is in the same directory only does not need to show the path


if (item. Absolutepath = = Model. Absolutepath)


       {


Return txt. Replace ("/" + RelativePath, item. Name);


       }


Else


       {


return txt. Replace ("/" + RelativePath, Getdeep (model). Level) + RelativePath);


       }


     }


static string tempdeep = @ ". /";


///<summary>


///The parent path by file depth


///</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>


///File Information entity


///</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;


     }


   }


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.