C # example code of absolute path splicing relative path

Source: Internet
Author: User

During project creation, we found that the Path. Combine method can only support directory splicing in the silly way.

Copy codeThe Code is as follows: // absolute path
String absolutePath = @ "C: \ Program Files \ Internet Explorer ";
// Relative path
String relativePath = @ ".. \ TestPath \";
// Expected splicing result
String splicingResult = string. Empty;
Console. writeLine (string. format ("Path. combine (\ "{0} \", \ "{1} \") = \ "{2} \" ", absolutePath, relativePath, Path. combine (absolutePath, relativePath )));

Output result:

The relative path and absolute path are not identified as expected, so we have to use regular expressions to match the relative path for re-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 splicing result
String splicingResult = string. Empty;
Console. writeLine (string. format ("Path. combine (\ "{0} \", \ "{1} \") = \ "{2} \" ", absolutePath, relativePath, Path. combine (absolutePath, relativePath )));
If (! Path. IsPathRooted (relativePath ))
{
// Match the relative path and the number of directory layers 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 );
// Match the file name, matching the number of directories to be appended
Regex = new Regex (@ "^ \ | ([a-zA-Z0-9] + )");
MatchCollection matches = regex. Matches (relativePath );
Foreach (Match match in matches)
{
Pathes. Add (match. Value );
}
// Obtain the drive address from the 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 result:

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.