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

Source: Internet
Author: User

System. Io. Path. Combine simply combines two path strings.
For example, for the following call, the string path returned by path. Combine (@ "C: \ 11", "aa.txt") is c: \ 11 \ aa.txt

The statement for 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 exception is thrown:
An unhandled exception of Type 'System. argumentnullexception 'occurred in mscorlib. dll
Additional information: value cannot be null.

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 exampleCode:
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 ));
The following are displayed in sequence:
\ 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.

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

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

Path. volumeseparatorchar Field
Provides platform-specific volume delimiters.
The value of this field is a colon (":") on Windows and Macintosh, and a slash ("/") on UNIX operating systems ("/").


Technorati: Asp.net

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.