Some Usage Details of the path. Combine (merge two path strings) Method

Source: Internet
Author: User

The declaration of this method is as follows: public static string combine (string path1, string path2)

When we merge some directories, the two parameters have some special restrictions. Let's look at these special restrictions in sequence.

1. If one of the parameters is null, an unhandled exception of Type 'System. argumentnullexception 'occurred in mscorlib. dll additional information: value cannot be null is thrown.

2. If one of the specified paths is a zero-length string, this method returns other paths. Of course, if both are zero-length strings, the return value is string. empty;

3. If path2 contains an absolute path, this method returns path2. For example, string TMP = path. Combine (@ "C: \ 11", @ "D: \ aa.txt"); TMP is equal to @ "D: \ aa.txt"

4. path2 cannot start with "\" or "/". If it starts with "/", "path2" is returned. For example, the following code: String p1 = "C: \ AA \"; string P2 = "C: \ AA ";

String p12 = "\ p12"; string P13 = "\ P13 \"; string P15 = "/P13 /";

Console. writeline (path. combine (P1, P12); console. writeline (path. combine (P1, P13); console. writeline (path. combine (P1, p15); console. writeline (path. combine (P2, P12); console. writeline (path. combine (P2, P13); console. writeline (path. combine (P2, p15); \ p12 \ P13 \/P13/\ p12 \ P13 \/P13/

How this function works

If path1 is not a drive reference (that is, it is not "C:" or "D:") and it is not ended with a valid separator defined in directoryseparatorchar, altdirectoryseparatorchar, or volumeseparatorchar, then, the directoryseparatorchar is appended to path1 before concatenation.

If path2 does not include the root (for example, if path2 does not start with a separator or drive specification), the result is a series of two paths with separators between them. If path2 contains the root, return path2.

Because no parameter analysis is performed when there is a blank space, if path2 is "C: \", it is appended to path1, instead of returning only path2.

Not all invalid characters in the directory and file name are interpreted as unacceptable by the combine method, because you can use these characters to search for wildcards. For example. combine ("C :\\","*. TXT ") may be invalid (if you want to create a file based on it), but it is valid as a search string. Therefore, the combine method successfully interprets it.

The path. directoryseparatorchar field provides platform-specific characters used to separate directory levels in path strings that reflect hierarchical file system organization. The value of this field is a slash ("/") on UNIX and a backslash ("\") on Windows and Macintosh operating systems ("\").

The path. altdirectoryseparatorchar field provides platform-specific replacement characters used to separate directory levels in path strings that reflect hierarchical file system organization. The value of this field is a backslash ("\") on UNIX and a slash ("/") on Windows and Macintosh operating systems ("/").

The path. volumeseparatorchar field provides the platform-specific volume separator. The value of this field is a colon (":") on Windows and Macintosh, and a slash ("/") on UNIX operating systems ("/").

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.