Reputation: 1795
I'm simply trying to retrieve all the files in a certain directory.
if (System.IO.Directory.Exists(dir_path)) //this line passes
{
//The following files array is empty although there's clearly files
string[] files = System.IO.Directory.GetFiles(dir_path);
}
Is there a way to copy over all the subdirectories, with all files still in their respective subdirectories when copied over?
Upvotes: 1
Views: 15045
Reputation: 353
I have encountered the same problem by trying to use System.IO.Directory.GetFiles()
and believing that it will return me all the immediate contents of a folder.
However, Directory.GetFiles()
only returns FILES. This means that subfolders will NOT be returned, only all the immediate files of a folder.
If you plan on getting all immediate files and folders of a directory you must use Directory.GetFileSystemEntries()
instead!
Upvotes: 0
Reputation: 2524
Try to run Visual Studio as an administrator. If the code runs successfully, then there may be some access privileges issue on the folder being accessed.
Upvotes: 0
Reputation: 23218
Based on your comment "The directory has 2 non-empty subdirectories", the Directory.GetFiles(string)
overload does not recursively check subdirectories and would not pick up those files.
Instead try using Directory.GetFiles(dir_path, "*", SearchOption.AllDirectories)
which will grab files in subdirectories as well.
Upvotes: 9