![](/img/trans.png)
[英]C# Get latest directory in a path - with respect to the the name of directory
[英]C# How do I get the name of a Directory instead of a path?
我试图从路径获取封闭目录:
DsVersions.ASSEMBLY2Row row = dsVersions.ASSEMBLY2.NewASSEMBLY2Row();
row.FOLDER = Path.GetDirectoryName(fileName);
我得到的是完整的路径 :
@"C:\Program Files (x86)\EdisonFactory\NetOffice\Plugins"
我需要的只是Plugins
。
我认为我需要使用Substring,但我不知道在哪里或如何。
您可以使用DirectoryInfo
获取帮助:
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(fileName));
row.FOLDER = dir.Name;
从你的路径你可以做到这一点:
new FileInfo(fileName).Directory.Name
实际上,快速做到这一点的方法是:
row.Folder = Path.GetFileName(Path.GetDirectoryName(filename));
GetFileName
只是获取最后一个分隔符后面的最后一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.