例如我在這個路徑下的文件ISample.cs像我們可以拆分C#中最後一個文件夾的文件路徑嗎?
"D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"
在這裏,我想從
"ProceduresAll\ISample.cs"
文件路徑在此之前,我不想說path.Here我是使用文件夾瀏覽器選擇文件夾。
請幫我解決這個問題。
例如我在這個路徑下的文件ISample.cs像我們可以拆分C#中最後一個文件夾的文件路徑嗎?
"D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"
在這裏,我想從
"ProceduresAll\ISample.cs"
文件路徑在此之前,我不想說path.Here我是使用文件夾瀏覽器選擇文件夾。
請幫我解決這個問題。
您的意思是這樣的?
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(使用上述獲得這些字符串)。
這應該工作:
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);
您既可以使用字符串函數分割拆就\或者你可以使用的LastIndexOf和子字符串函數在路徑砍了。
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
不包含文件名了。
來自MSDN(`GetDirectoryName`):在大多數情況下,此方法返回的字符串由路徑中所有字符組成,但不包括最後一個DirectorySeparatorChar或AltDirectorySeparatorChar。 – Oded 2010-12-01 11:37:14
抱歉它不能正常工作 – 2010-12-01 11:41:22