Is it possible to split Filepath from the last folder in C #?

For example, I have an ISample.cs file in this path, for example

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

Here I want the file path from

"ProceduresAll\ISample.cs"

Before that I do not want this path. I use the folder browser to select a folder.

Please help me with this.

+3
source share
4 answers

Do you mean 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 ("Procedures", "ISample.cs") to get "ProceduresALL \ ISample.cs" (using the above to get these lines).

+16
source
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 part of the directory, because it filePathno longer contains the file name.

+2
source

Split \, LastIndexOf Substring, .

0

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);
-1
source

Source: https://habr.com/ru/post/1777893/


All Articles