简体   繁体   中英

How Do I filter out names of folders in C#?

I have the code searching through the directory and picks out all the folders, but I only want it to pick out ones that Start with Data. How would I do that?

Below is the code I have that goes through the Directory:

    string[] filePaths = Directory.GetDirectories(defaultPath).Where(Data => !Data.EndsWith(".")).ToArray();

No need to use LINQ; GetDirectories supports search patterns, and will probably be significantly faster since the filtering may be done by the filesystem, before enumerating the results in .NET.

string[] filePaths = Directory.GetDirectories(defaultPath, "Data*");

Note that * is a wildcard which matches zero or more characters.

If "starts with data" you just mean the folder name begins with "Data", this will work

string[] filePaths = Directory.GetDirectories(defaultPath)
    .Where(s => s.StartsWith("Data") && !s.EndsWith(".")).ToArray();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM