Assume I have an incomplete path-string: C:\dir\temp\f And I have the following filesystem:

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 the best matching. In this example it would be c:\dir\temp\foobar\ because it starts with the path-string (and foobar matches better than foobar2). how can i efficiently get the available path? Do I have to split up the incomplete path string by / and test if the folders are available or is there a better method?

有帮助吗?

解决方案

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

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

This will return a collection of all matching directories, so you will have to figure out which one of them matches best by yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top