When doing the project found that Path.Combine method can only support the Fool-type catalog stitching
Copy Code code as follows:
Absolute path
String absolutepath = @ "C:\Program files\internet Explorer";
Relative path
string relativepath = @ ". \testpath\ ";
Expected stitching results
String Splicingresult = String. Empty;
Console.WriteLine (String. Format ("Path.Combine" (\ "{0}\", \ "{1}\") =\ "{2}\", Absolutepath, RelativePath, Path.Combine (Absolutepath, RelativePath)));
The output results are:
found that the relative path and absolute path are not identified according to the imaginary, so we have to use the matching relative path to do the stitching, the following methods only support absolute path + relative path
Absolute path
String absolutepath = @ "C:\Program files\internet Explorer";
Relative path
string relativepath = @ ". \testpath\ ";
Expected stitching results
String Splicingresult = String. Empty;
Console.WriteLine (String. Format ("Path.Combine" (\ "{0}\", \ "{1}\") =\ "{2}\", Absolutepath, RelativePath, Path.Combine (Absolutepath, RelativePath)));
if (! Path.ispathrooted (RelativePath))
{
Matches the relative path, matching the number of directory layers that need to be pushed up
Regex regex = new Regex (@ ^\\|) ( [..] +)");
int backUp = regex. Matches (RelativePath). Count;
list<string> pathes = absolutepath.split ("\"). ToCharArray ()). ToList ();
Pathes. RemoveRange (pathes. Count-backup, backUp);
Matching file names, matching the number of directory layers that need to be added
regex = new Regex (@ "^\\|" ( [a-za-z0-9]+)];
MatchCollection matches = regex. Matches (RelativePath);
foreach (match match in matches)
{
Pathes. ADD (match. Value);
}
Drive address to drive address in absolute path
Pathes[0] = Path.getpathroot (Absolutepath);
foreach (string p in pathes)
{
Splicingresult = Path.Combine (Splicingresult, p);
}
}
Console.WriteLine (String. Format ("Absolute path={0}", Absolutepath));
Console.WriteLine (String. Format ("Relative path={0}", RelativePath));
Console.WriteLine (String. Format ("Path.Combine" (\ "{0}\", \ "{1}\") =\ "{2}\", Absolutepath, RelativePath, Splicingresult));
Console.ReadLine ();
Output results: