C # Absolute path stitching relative path Instance Code _c# tutorial

Source: Internet
Author: User

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:

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.