惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - lanjue

印度同行成功者给我的忠告 创业型企业如何招聘销售总监 支付宝常见问题说明 读《度过有意义的生命》有感 克菲勒留给儿子的38封信 戴尔借Twitter交流平台获100万美元销售收入 右键新建菜单中增加新类型文件 前台和后台线程 如何衡量两个词的相关度 三个在线生成".ico"图片的网站 - lanjue - 博客园 小结提升网站访问量 网页设计工具补丁 各种免费在线工具收集 ip计算实例 改善用户体验的alert提示效果 教你一招又叠衣服 转《浅谈数据库设计技巧》 转《“数据建模”读书笔记 》 转<数据库设计经验>
Path.Combine (合并两个路径字符串)方法的一些使用细节
lanjue · 2006-11-21 · via 博客园 - lanjue

System.IO.Path.Combine 简单来说,就是合并两个路径字符串。
比如如下调用,Path.Combine(@"C:\11","aa.txt") 返回的字符串路径如后: C:\11\aa.txt

这个方法的声明如下:
public static string Combine ( string path1, string path2 )

我们在合并一些目录的时候,它的两个参数有些特殊限制,下面我们就来依次看这些特殊限制

1、如果其中一个参数为 null ,会抛出异常:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.

2、如果指定的路径之一是零长度字符串,则该方法返回其他路径。当然,两个都是零长度字符串,则返回的就是 string.Empty ;

3、如果 path2 包含绝对路径,则该方法返回 path2。
比如:string tmp = Path.Combine(@"C:\11", @"D:\aa.txt");
tmp 就等于 @"D:\aa.txt"

4、 path2 不能以 \ 和 / 开头的字符串, 如果是这个字符串开头的,则返回 path2 。
比如以下代码:
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/

该函数工作原理

如果 path1 不是一个驱动器引用(即不是"C:"或"D:")而且不是以 DirectorySeparatorChar、AltDirectorySeparatorChar 或 VolumeSeparatorChar 中定义的有效分隔符结束,则在串联前将 DirectorySeparatorChar 追加到 path1 中。

如果 path2 不包括根(例如,如果 path2 没有以分隔符或驱动器规格起始),则结果是两个路径的串联,具有介于其间的分隔符。如果 path2 包括根,则返回 path2。

由于有空白时不进行参数分析,如果 path2 为" c:\\ ",则将其追加到 path1,而不是仅返回 path2。

不是目录和文件名的所有无效字符都被 Combine 方法解释为不可接受的,因为您可以将这些字符用于搜索通配符。例如,尽管 Path.Combine("c:\\", "*.txt") 可能是无效的(如果您要根据它创建一个文件),但它作为搜索字符串是有效的。因此 Combine 方法成功解释它。

Path.DirectorySeparatorChar 字段
提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
该字段的值在 Unix 上为斜杠("/"),在 Windows 和 Macintosh 操作系统上为反斜杠("\")。

Path.AltDirectorySeparatorChar 字段
提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
该字段的值在 Unix 上为反斜杠("\"),在 Windows 和 Macintosh 操作系统上为斜杠("/")。

Path.VolumeSeparatorChar 字段
提供平台特定的卷分隔符。
该字段的值在 Windows 和 Macintosh 上为冒号(":"),在 Unix 操作系统上为斜杠("/")。