我有 :
string Combine = Path.Combine("shree\\", "file1.txt");
string Combine1 = Path.Combine("shree", "file1.txt");
Run Code Online (Sandbox Code Playgroud)
两者给出相同的结果:
shree\file1.txt
背后究竟发生了 Path.Combine什么?这是最好的编码实践.请清楚我的愿景.谢谢.
如果第一个路径(shree或shree \\)没有以有效的分隔符(例如DirectorySeparatorChar)结尾,则会在连接之前将其附加到路径.
所以
string path1 = "shree";
string path2 = "file1.txt";
string combined = Path.Combine(path1, path2);
Run Code Online (Sandbox Code Playgroud)
将导致"shree\file1.txt",而
string path1 = "shree\\";
Run Code Online (Sandbox Code Playgroud)
已包含有效的分隔符,因此Combine方法不会添加另一个.
在这里,您在字符串变量(path1)中键入两个斜杠.第一个只是第二个作为转义字符.这与使用逐字字符串文字相同.
string path1 = @"shree\";
Run Code Online (Sandbox Code Playgroud)
有关Combine方法的更多信息可以在MSDN上找到:
http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |