基于為何我不喜歡用Path.Combine的詳解
Path.Combine:
什么時(shí)候會(huì)用到Path.Combine呢?,當(dāng)然是連接路徑字符串的時(shí)候!
所以下面的代碼可以完美的工作:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
結(jié)果如下:
當(dāng)然你也可以:
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, "def", pb));
結(jié)果是:
從這個(gè)例子可以知道,我們不需要考慮arr_pa里面的字符串是不是以”\” 結(jié)尾,這的確提供了方便,而且這也是很多人喜歡使用Path.Combine的一個(gè)原因,但是僅此而已。
Path.Combine 雖然解決了路徑連接問題,但是由于很多人片面的去理解它,所有它非常容易造成錯(cuò)誤的應(yīng)用,要想用好Path.Combine 并非易事,下面我會(huì)列舉幾個(gè)實(shí)例來說明這點(diǎn)。
第一個(gè):當(dāng)path2 是相對(duì)路徑的時(shí)候,返回的是path2,path1會(huì)被丟棄。
看一下下面的代碼:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
你知道這段代碼輸出什么嗎?
這段代碼的輸出如下:
可以看到對(duì)于”/test.txt” 和”\test.txt” ,Path.Combine 認(rèn)為path2是相對(duì)路徑,所以直接返回path2.。
第二點(diǎn):路徑是驅(qū)動(dòng)器,返回的結(jié)果不正確
public static void Main()
{
string[] arr_pa = { @"c:", @"c:\" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, pb));
}
}
}
輸出結(jié)果是:
可以看到,如果path1 是” C:”的話,那么Path.Combine結(jié)果就是不正確的。
第三點(diǎn):無法連接http路徑
除了連接本地路路徑之外,有的時(shí)候,也需要拼接http鏈接地址,可惜的是System.IO.Path.Combine卻無法拼接http地址。
將arr_pa 修改為
string[] arr_pa = { @"http://www.Test.com/", @"http://www.Test.com" };
結(jié)果如下:
在這里就沒有什么技巧了,純粹的死記硬背,
記住,只有
才會(huì)產(chǎn)生正確的解。
如果你將代碼修改為:
public static void Main()
{
string[] arr_pa = { @"http://www.Test.com/", @"http://www.Test.com" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, Path.Combine(pa, "def", pb));
}
}
}
那么無論怎樣,你都無法得到正確的結(jié)果:
正是因?yàn)樯鲜龅膸c(diǎn)不足,導(dǎo)致Path.Combine 很難用,這也是有一部分人選擇使用String.Format 的原因了。
當(dāng)然,我寫了一個(gè)MyPath.Combine的方法,可以解決上面的問題。 string firstPath = paths[0]; if (firstPath.StartsWith("HTTP", StringComparison.OrdinalIgnoreCase)) if (!firstPath.EndsWith(spliter)) for (int i = 1; i < paths.Length; i++) if (i != paths.Length - 1)//not the last one builder.Append(nextPath); return builder.ToString();
class MyPath
{
public static string Combine(params string[] paths)
{
if (paths.Length == 0)
{
throw new ArgumentException("please input path");
}
else
{
StringBuilder builder = new StringBuilder();
string spliter = "\\";
{
spliter = "/";
}
{
firstPath = firstPath + spliter;
}
builder.Append(firstPath);
{
string nextPath = paths[i];
if (nextPath.StartsWith("/") || nextPath.StartsWith("\\"))
{
nextPath = nextPath.Substring(1);
}
{
if (nextPath.EndsWith("/") || nextPath.EndsWith("\\"))
{
nextPath = nextPath.Substring(0, nextPath.Length - 1) + spliter;
}
else
{
nextPath = nextPath + spliter;
}
}
}
}
}
}
使用也比較簡(jiǎn)單
例如:
public static void Main()
{
string[] arr_pa = { @"c:\abc\", @"c:\abc", @"http://www.Test.com/", @"http://www.Test.com" };
string[] arr_pb = { @"\test.txt", @"/test.txt", @"test.txt" };
foreach (string pa in arr_pa)
{
foreach (string pb in arr_pb)
{
Console.WriteLine("'{0}' + '{1}'= '{2}'", pa, pb, MyPath.Combine(pa, pb));
}
}
}
結(jié)果如下:
當(dāng)然,你也可以這樣:
Console.WriteLine("{0}+{1}+{2}+{3}={4}",
pa, "p2", "\\p3/", pb, MyPath.Combine(pa, "p2", "\\p3/", pb));
輸出如下:
最后如果你不用Path.Combine,那么還可以選擇string.Format
相關(guān)文章
C#實(shí)現(xiàn)自由組合本地緩存、分布式緩存和數(shù)據(jù)查詢
這篇文章介紹了C#實(shí)現(xiàn)本地緩存、分布式緩存和數(shù)據(jù)查詢的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Unity向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Unity向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn)
這篇文章主要為大家介紹了Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08C#中比較常用的DateTime結(jié)構(gòu)的使用方法
這篇文章主要介紹了C#中比較常用的DateTime結(jié)構(gòu)的使用方法,需要的朋友可以參考下2015-11-11C#開發(fā)微信門戶及應(yīng)用(3) 文本消息和圖文消息應(yīng)答
這篇文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第二篇,微信文本消息和圖文消息的應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Unity 實(shí)現(xiàn)給物體動(dòng)態(tài)添加事件
這篇文章主要介紹了Unity 實(shí)現(xiàn)給物體動(dòng)態(tài)添加事件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04