2012-02-15 121 views
9

使用Path.GetFullPath可以使用雙反斜槓獲得完整路徑嗎?事情是這樣的:雙反斜槓(C#)的完整路徑

C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt 

,而不是這樣的:

C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt 

或者有沒有其他的方法?

+2

也許'string.Replace'與'@ 「\」'到'@「\\」'? – 2012-02-15 13:18:11

+1

爲什麼你需要它這樣?無論如何,你可以輕鬆地用\\ \\ \\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ – Aamir 2012-02-15 13:18:42

+1

只需將「\」替換爲「\\」? – Stefan 2012-02-15 13:18:45

回答

17

你的意思是?

Path.GetFullPath(path).Replace(@"\", @"\\"); 
+0

根據他的要求,如果'Path.GetFullPath'返回像「@」\\ myserver \ myshare \ some \ file.txt「'這樣的UNC路徑,這可能會導致意想不到的結果。 – 2012-02-15 13:19:58

+1

我同意@Uwe - 不知道爲什麼你想這樣做,但嘿 - 這是一個答案! :) – greg84 2012-02-15 13:21:10

3

C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt是不是一個有效的路徑,所以我不知道爲什麼你要它,但:

Path.GetFullPath(yourPath).Replace("\\", "\\\\"); 
+0

string.Replace也將char作爲參數。 '.Replace('\','\\')'應該就夠了。 – nawfal 2014-01-11 01:11:48

+0

@nawfal'\\'不是字符。 – 2015-04-08 08:57:14

+1

@BurakKarakuş你是對的。當我發表評論時,我不知道我在想什麼...... – nawfal 2015-04-08 09:17:50

0

你可以只是這樣做:

Path.GetFullPath(@"C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt") 

但我不知道爲什麼,你想逃避\?

如果是的話,你可以做這樣的:

Path.GetFullPath(@"C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt") 
0

我建議做一個與string.replace()。我最近不得不在一個項目中爲自己做這件事。所以,如果你做類似的東西:

String input = Path.GetFullPath(x); 
input = input.Replace("\\","\\\\"); 

我相當有信心,是你所需要的:)

文檔: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

相關問題