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