I have the following full path:
D:\\Test1\\Test1_1\\Test2_1\\Test3_1\\Test4_1
If I call
Path.GetFileName(abovePath)
will return Test4_1
Well.
I want to get Test2_1\\Test3_1
, for example.
How to do that with Path
class if is possible ? If not, how to proceed ?
I want a generic example, do not focus only my given full path.
Thank you.
Use DirectoryInfo and Parent, eg
var directoryInfo = new DirectoryInfo(abovePath);
var parentDirectoryName = directoryInfo.Parent.Name;
var grandParentDirectoryName = directoryInfo.Parent.Parent.Name;
Note you would want null checks on these but that's the general idea
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.