Define Existing Path Directory

Suppose I have an incomplete path line: C:\dir\temp\f And I have the following file system:

 C:\dir\ c:\dir\temp\ c:\dir\temp\foobar\ c:\dir\temp\foobar2\ c:\dir\temp\bar 

I want to determine which part of the directory is best suited. In this example, it will be c:\dir\temp\foobar\ because it starts with a path string (and foobar matches better than foobar2 ). How can I effectively get an accessible path? Should I split the incomplete path line by / and check if the folders are accessible or is there a better method?

+4
source share
1 answer

Basically, what you want to do is something like dir C:\dir\temp\f* . In .Net, you can do this using Directory.GetDirectories() , but this requires breaking the full path into the path and template. You can use the methods from the Path class to do this. So your code might look something like this:

 Directory.GetDirectories( Path.GetDirectoryName(path), Path.GetFileName(path) + "*") 

This will return a collection of all the relevant directories, so you will need to figure out which one works best for you.

+3
source

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


All Articles