1

I am trying to get all files from C:/ drive from all directories but I am getting an odd error saying " System.UnauthorizedAccessException: 'Access to the path 'c:\Documents and Settings' is denied.'

would you check my snippet code and let me know how I can fix that

        internal static string[] DirSearch(string sDir)
{
   string[] Files = Directory.GetFiles(UserDocumentsDir, "*", SearchOption.AllDirectories); //Getting Text filesstring[] Files 
            return Files;
}

string[] files =  CleanHelper.DirSearch(@"c:\");
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    User you're running application as doesn't have access(permissions) to the folder. Change folder security options, or run application as other user (e.g. administrator), that has access to it. – Vytautas Plečkaitis Aug 15 '19 at 11:47

1 Answers1

0

This is a question of NTFS security, NTFS use and user rights, not programming.

A number of folders are protected from access by any user. Stuff like the root directory of the main partition (C:\), both Programm directories, the Windows folder, etc. Often you still can read some of them, but do not expect being able to write them. You need to run the programm elevated to have any shoot of access to those folders. However this should not be such a case, as Users or Documents and Settings (depending on OS version) should be open at least to the top layer.

Another issue are Symbolic Links. They are basically redirects within the Filesystem, and are used for fallback names. On the one hand, you need to properly deal with them to resolve them to the real name. On the other hand, blindly itterating over symbolic links will get you into a infinite loop as sometimes symbolic links referer to the folder you find them in. My advise is to just ignore them for the beginning.

And course depending on what you really want to do with those folders and files, something pre-written like Robocopy would be the better thing to use.

Christopher
  • 9,634
  • 2
  • 17
  • 31