Path.Combine 的两个参数,均不允许为 null,另外,只要其中一个参数为零长度字符串,就直接返回另一个参数。
string s1 = Path.Combine("D:", "cftea.txt"); // D:cftea.txt string s2 = Path.Combine("D:\", "cftea.txt"); // D:cftea.txt string s3 = Path.Combine("D:\dir", "cftea.txt"); // D:dircftea.txt string s4 = Path.Combine("D:\dir\", "cftea.txt"); // D:dircftea.txt string s5 = Path.Combine("D:", "/cftea.txt"); // /cftea.txt string s6 = Path.Combine("D:\", "\cftea.txt"); // cftea.txt string s7 = Path.Combine("D:\dir\", "/cftea.txt"); // /cftea.txt string s8 = Path.Combine("D:\dir\", "\cftea.txt"); // cftea.txt
答案都在备注中,关键是注意第 1 个和最后 4 个。
相关阅读
C# 的 Path.GetFileName、Path.GetExtension、Path.GetDirectoryName