I'm trying to read a directory using C# and return all the folder names in that directory. I do not want any nested folders or files to be returned, only the immediate folders in the directory.
I'm going to use these names to build a side nav in my application. Users will have the ability to create new folders so I want the nav to be dynamically built as new folders get created over time. I have tried to different solutions thus far.
1st - I used Directory.GetFiles() and looped through directory but this returns every single folder and file name found in the whole directory.
2nd - I used Directory.GetDirectory() and tried looping through the directory and this does return just the folder name, but only the first folder! I need the remaining folders. It's like my loop is not working.
1st Try:
string [] files = Directory.GetFiles(@"\\\\SERVERNAME\\Data\\ServerLinks\\Safety\\","*",SearchOption.AllDirectories);
string File = "";
for (int ii = 0; ii < files.Length; ii++)
{
File += "|" + files[ii];
}
return (File);
2nd Try:
string path = @"\\\\SERVERNAME\\Data\\ServerLinks\\Safety\\";
foreach (string folder in Directory.GetDirectories(path))
{
string name = folder.Remove(0, folder.LastIndexOf('\\') + 1);
return (name);
}
return ("");