daily pastebin goal
3%
SHARE
TWEET

get files in directory

kyrathasoft Jun 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //obtain the directory we’re wanting to search for files…
  2. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
  3.     folderToSearch = folderBrowserDialog1.SelectedPath; //make folderToSearch a form-level public variable
  4. }
  5.  
  6. //if we want only top-level files (those not in subfolders of folderToSearch), then…
  7. DirectoryInfo di = new DirectoryInfo(folderToSearch);
  8. FileInfo[] fi = di.GetFiles(*.*”, SearchOption.TopDirectoryOnly);
  9.  
  10. //if we want to include subdirectories in our search:
  11. DirectoryInfo di = new DirectoryInfo(folderToSearch);
  12. FileInfo[] fi = di.GetFiles(*.*”, SearchOption.AllDirectories);
  13.  
  14. //now the fi array instance contains full paths of all found files (let’s put filenames in a listbox):
  15. lstFiles.Items.Clear();
  16.  
  17. foreach (FileInfo filinf in fi) {
  18.     lstFiles.Items.Add(Path.GetFileName(filinf.FullName));
  19. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top