I have a path for example C:/desktop and i wanna know if in this path are any folders named like folder A, folder B... i dont care whats in them i just need to know if they exist. ive seen the similar questions being asked but they were about splitting the path name, but the only path name i have is the most basic one, in those other question the path name was like, c:/desktop/folderA/folderB i wanna just know if C:/desktop/folderA exists or not and do it with a bunch of other folder names
-
there's a [`Directory.Exists`](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.exists?view=net-5.0) method you can use. Can you show an example of what you currently have as input and some pseudo code of how you intented to use it? – default Apr 28 '21 at 14:06
7 Answers
Have you tried GetDirectories()
?
var folders = System.IO.Directory.GetDirectories(@"C:/desktop");

- 11,464
- 3
- 53
- 79
You can use an overload on the GetDirectories
mentioned by @mxmissile to make it explicit you want to search to top directories only.
System.IO.Directory.GetDirectories("C:/desktop", "*", SearchOption.TopDirectoryOnly)

- 371
- 4
- 15
You can search through a folder with System.IO.Directory.GetDirectories
In your case you should find your "Folder A" with:
Directory.GetDirectories(@"C:\desktop\", "folderA");

- 36
- 1
- 5
You can try to check if Directory.Exists()
. If you already know which folders you're looking for, you could do this:
var main_dir = @"C:\desktop\";
string[] folders = {"FolderA", "FolderB"};
string[] dir = new string[folders.Length];
for(int i = 0; i < folders.Length; ++i)
{
dir[i] = Path.Combine(main_dir, folders[i]);
Console.WriteLine(dir[i]);
Console.WriteLine(Directory.Exists(dir[i]));
}

- 325
- 4
- 14
Here is what you ask for. I have added a foreach to get all cases (folder A, b, c ...)
bool directoryExist = System.IO.Directory.Exists(@"c:\desktop\forder A");
foreach (var folderPath in System.IO.Directory.GetDirectories(@"c:\desktop"))
{
string folderName = System.IO.Path.GetDirectoryName(folderPath);
if (folderName == "Folder A") { //Case Sensible
}
}

- 1,109
- 14
- 29
You can try querying with a help of Linq:
using System.IO;
using System.Linq;
...
// Do we have in root
string root = @"C:/desktop";
// Any subdir(s) mentioned
HashSet<string> subdirs = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
@"folder A",
@"folder B",
};
bool contains = Directory
.EnumerateDirectories(root, "*", SearchOption.TopDirectoryOnly)
.Any(dir => subdirs.Contains(new DirectoryInfo(dir).Name));
Change SearchOption.TopDirectoryOnly
into SearchOption.AllDirectories
if you want deep search (if root
itself or its any subdirectory has a folder mentioned in subdirs
). You can alter the query to asnwer different questions:
// If all subdirs exist:
bool conatinsAll = Directory
.EnumerateDirectories(root, "*", SearchOption.TopDirectoryOnly)
.All(dir => subdirs.Contains(new DirectoryInfo(dir).Name));
// Names of subdirs exist
string[] existingSubDirs = Directory
.EnumerateDirectories(root, "*", SearchOption.TopDirectoryOnly)
.Where(dir => subdirs.Contains(new DirectoryInfo(dir).Name))
.ToArray();

- 732,580
- 175
- 1,330
- 1,459

- 180,369
- 20
- 160
- 215