Advertisement
kyrathasoft

get files in directory

Jun 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement