You can use DirectoryInfo.GetFiles("C:\*.*", SearchOption.AllDirectories);
This will return an array of FileInfo
objects, which include a Name and FullName property for the filename.
That being said, I would not do this on "C:\", as you'll return a huge array, but this technique will work correctly on an appropriate folder.
If you're using .NET 4, I'd recommend using EnumerateFiles instead, which will return an IEnumerable<T>
instead of an array.
Note that the above code will most likely fail, however, as it requires full permissions to search the file system. A SecurityException will be raised if you can't access specific parts of the file system you're trying to search.
Also - if you're only interested in the file names and not the full FileInfo
information, you can use Directory.EnumerateFiles instead, which returns an IEnumerable<string>
with all of the relevant filenames.