开发者

Can we split the Filepath from the last folder in C#?

For exam开发者_StackOverflowple 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.


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).


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜