简体   繁体   中英

C# How do I get the name of a Directory instead of a path?

I am trying to get the enclosing directory from a path:

DsVersions.ASSEMBLY2Row row = dsVersions.ASSEMBLY2.NewASSEMBLY2Row();
row.FOLDER = Path.GetDirectoryName(fileName);

What I get is the full path :

@"C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins"

And what I need is just Plugins .

I think that I need to use Substring but I am not sure where or how.

You could use DirectoryInfo for help:

DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(fileName));
row.FOLDER = dir.Name;

从你的路径你可以做到这一点:

new FileInfo(fileName).Directory.Name

Actually, a quick way of doing it is:

row.Folder = Path.GetFileName(Path.GetDirectoryName(filename));

GetFileName simply gets the last string after the last separator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM