-1

In my code, there is a button in the form, "Select Folder". When the user click on this button, a Browser dialog box appears.

I want user to select a directory from this Browser. After that, I want to keep the number of folder (only folders, not other file types) in a variable.

I use FolderBrowserDialog function. How can I do this?

Salim Azak
  • 79
  • 10
  • 2
    you need to get the folders or files in that directory chosen using the Directory.GetFiles method or Directory.GetDirectories() method: https://msdn.microsoft.com/en-us/library/c1sez4sc(v=vs.110).aspx and simply do a count or a length - 1 call. Also check this: https://msdn.microsoft.com/en-us/library/ms143314(v=vs.110).aspx – Ahmed ilyas Mar 26 '15 at 15:30
  • FolderBrowserDialog fbd = new FolderBrowserDialog(); num_channels = Directory.GetDirectories(@"fbd.SelectedPath").Length; this didnot work – Salim Azak Mar 27 '15 at 09:08
  • what "didn't" work?? I don't see where you showed your fbd.... – Ahmed ilyas Mar 27 '15 at 10:02

2 Answers2

0

You can use this;

var directoryInfo = new System.IO.DirectoryInfo(@"D:\path\");

int directoryCount = directoryInfo.GetDirectories().Length;

If you want more help about filtering folders in a directory without files, inform me.

Salim Azak
  • 79
  • 10
MeshBoy
  • 662
  • 5
  • 9
  • ' var directoryInfo ' didnot work. It gives error. Beginning of my code is like that:
    'FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.MyComputer; fbd.Description = "Select the folder including all channels"; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) ' Could you modify your suggestion according to this?
    – Salim Azak Mar 27 '15 at 08:52
0

https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx

FolderBrowserDialog is a modal dialog box. Therefore, when shown, it blocks the rest of the application until the user has chosen a folder. When a dialog box is displayed modally, no input (keyboard or mouse click) can occur except to objects on the dialog box. The program must hide or close the dialog box (usually in response to some user action) before input to the calling program can occur.

Basically, you can't communicate anything back to the form which opened the dialog box. If you need to do that, you better off writing a custom control.

Salim Azak
  • 79
  • 10
KonB
  • 220
  • 1
  • 10