Finding specific directories in C#.NET?
We have a concret directiory (e.g. "C:\personal\app\cherry\") where while the runtime of another application a folder with 2 significant information in its name will be generated randomly. One of those information will remain constant, everytime the folder is generated. As well the folder will be removed while runtime too, but this is not really relevant in this case. So there would be a folder with two information split with a simple dot.
Example: \oskdfo.chips\
Where oskdfo is the randomly generated part, and chips will be the constant. So the constant is the information we need to find this specific directory, hence the other information will never remain the same, a uncommon way to find the actual position of this directory is needed here.
So now I'm searching for a procedure to find t开发者_运维知识库his directory with this specific format inside a given path, where also all subdirectories should be included for the search.
You never said if the directory is created under your application path or if you want to search the entire hard drive.
Anyway you should use Directory.GetDirectories
method to search for it. The return value is an array with all directories that can be found in the specified path.
You can get all folder in the app path by using the following:
var folders = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory)
With LINQ you can narrow it down:
var folders = Directory
.GetDirectories(AppDomain.CurrentDomain.BaseDirectory)
.Where(folder => folder.Contains("usuall")
.ToList();
精彩评论