So I've used this site for some VBA stuff I did in the past and found some great help by other users' questions. Now I've begun using C# and am totally lost on how to get the details from a file. I will admit, I am completely new to C# and am having a pretty difficult time getting used to it. What I'm trying to do is choose a directory (already have that in place with FolderBrowserDialog), and then grab all the names of the folders in that directory, and all of the subfolders. To put it in to context, I want to go to my Music folder, and then be able to make a list of all the artists and albums in that directory in a textbox. Each album will be under the Artist folder, so the whole thing would read the name of one folder, then all the subfolders in that folder, then go back and move on to the next one.
Not sure if anyone will ask, but here is all the code I have:
public Form1()
{
InitializeComponent();
}
private void DirButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK);
{
DirBox.Text = fbd.SelectedPath;
}
}
Any help is greatly appreciated and I'll try to understand your answer(s) as best as I can.
Thanks!
One of the many options would be to use
Directory.GetDirectories
, see docsYour code could look something like this:
Depending on the structure of your folders, you might need to use another
SearchOption Enumeration
- use 'AllDirectories' to do a recursive search, orTopDirectoryOnly
to look only in the top directory, see docsNow, if you don't need a recursive search you can use a simpler overload
DirectoryInfo.GetDirectories
as described here, sample follows:Please note that you can do the same by using
DirectoryInfo
class instead ofDirectory
class if you prefer to have an instance rather than working with statics, be sure to check out the docs