-1

Is there a way to go through folders in directory? I couldn't find a solution for this, here's an example:

While (found folder) 
{
   Go to the folder

   If (there is no folder to go in) 
   {
      Go back from folder and skip the first folder
      where you were before and go to next if found
      and if not then go back, and so on... 
   } 
} 

I hope that's enough of explanation.

By the way: I need this for antivirus and I know, to build an antivirus you shouldn't use c# let's say that would be for personal use.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
d2dyno
  • 15
  • 2
  • 6
  • Have a look at [Directory.GetDirectories](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.geDirectory.GetDirectories.7.2) – H.G. Sandhagen Jan 01 '19 at 16:49
  • H g sandhagen probably means https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getdirectories?view=netframework-4.7.2 - maybe some typo in his URL – Caius Jard Jan 01 '19 at 16:56

1 Answers1

0

It's not really clear what you want but I assume you want to scan a hard disk folder by folder, getting a list of files in every folder.

If all you want is a list of all files, you can just ask Directory.GetFiles("c:\\", "*", SearchOption.AllDirectories) and it will take some time then give you a single massive list of every file. Corollorary methods named like EnumerateFile* do a similar thing, returning you an enumerator over a collection of filepath strings or FileSystemInfo objects

If you want some more fine grained control over which directories, Directory also has methods to Get/Enumerate Directories - the overload that takes a string,string,SearchOption allows you to specify whether you want just the named directory to be listed, or whether you want the runtime to search all subdirectories for you. Which you pick will probably depend on how you want to give feedback to the user in regards to which directory is currently being searched - Get* methods wait until the entire search has completed before giving you the results; you have to wait but then you know have dactyl how many files you're dealing with (progress bar?), Enumerate* return results sooner (while the process of searching the disk is ongoing). but you don't know in advance the full list so you can only show what you're currently doing, not how far you've got/to go

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • How can I bypass such things: System.UnauthorizedAccessException: 'Access to the path 'c:\$Recycle.Bin\S-1-5-18' is denied.' My code: string[] files = Directory.GetFiles("c:\\", "*", SearchOption.AllDirectories); string s = ""; for (int i = 0; i <= files.Length; i++) s += files[i] + "\n"; MessageBox.Show(s); – d2dyno Jan 01 '19 at 18:26
  • The user will have to launch your program with administrative privileges. There may still be some directories that are inaccessible due to how windows has structured the ownership/security – Caius Jard Jan 02 '19 at 09:22