Can we split the Filepath from the last folder in

2020-07-23 04:13发布

For example i have a file ISample.cs in this path like

"D:\TEST_SOURCE\CV\SOURCE CODE\ARMY.Data\ProceduresALL\ISample.cs"

Here i wanna the file-path from

"ProceduresAll\ISample.cs"

Before that i don't wanna that path.Here i am using folder browser for choosing the folder.

Please help me regarding this.

标签: c#-4.0
4条回答
地球回转人心会变
2楼-- · 2020-07-23 04:33

You mean like this?

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();

Use Path.Combine("ProceduresALL", "ISample.cs") to get ProceduresALL\ISample.cs (using the above to get these strings).

查看更多
The star\"
3楼-- · 2020-07-23 04:38

You could either use the string function Split to split on \ or you could use the LastIndexOf and Substring functions to chop away at the paths.

查看更多
做个烂人
4楼-- · 2020-07-23 04:45
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) gets the "filename" part which is actually the last directory part as filePath doesn't contain a filename anymore.

查看更多
走好不送
5楼-- · 2020-07-23 04:57

This should work:

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);
查看更多
登录 后发表回答