2010-12-01 63 views

回答

16

您的意思是這樣的?

string path = @"D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"; 

//ISample.cs 
Path.GetFileName(path); 

//D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL 
Path.GetDirectoryName(path); 

//ProceduresALL 
Path.GetDirectoryName(path).Split(Path.DirectorySeparatorChar).Last(); 

使用Path.Combine( 「ProceduresALL」, 「ISample.cs」),以獲得ProceduresALL \ ISample.cs(使用上述獲得這些字符串)。

-1

這應該工作:

var path = "D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"; 
var fileName = System.IO.Path.GetFileName(path); 
var directoryName = System.IO.Path.GetDirectoryName(path); 
System.IO.Path.Combine(directoryName,fileName); 
+0

來自MSDN(`GetDirectoryName`):在大多數情況下,此方法返回的字符串由路徑中所有字符組成,但不包括最後一個DirectorySeparatorChar或AltDirectorySeparatorChar。 – Oded 2010-12-01 11:37:14

+0

抱歉它不能正常工作 – 2010-12-01 11:41:22

0

您既可以使用字符串函數分割拆就\或者你可以使用的LastIndexOf和子字符串函數在路徑砍了。

2
string fullPath = @"D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"; 
string fileName = Path.GetFileName(fullPath); 
string filePath = Path.GetDirectoryName(fullPath); 
string shortPath = Path.Combine(Path.GetFileName(filePath), fileName) 

Path.GetFileName(filePath)得到「文件名」其中一部分實際上是最後一個目錄一部分filePath不包含文件名了。

相關問題