Dave
Dave

Reputation: 8451

C# - Cannot access all files

My application uses the .NET object Directory.GetFiles()

The actual overload I'm using is

var allFiles = Directory.GetFiles("C:\\Users\\Dave", "*.*", SearchOption.AllDirectories);

The issue is when the source folder is C:\Users\UserName as it then tries to look through application data folder.

When it tries to read from the application data folder, an exception is thrown:

"Access to the path 'C:\Users\Dave\AppData\Local\Application Data' is denied."

So, my question is does any one have an opinion as to my options? I would assume I have to either change the way I collect all the files or, there may be a built in overload or method which will allow me to continue this (which I clearly don't know about).

If it helps, the goal of this is to take all the files retrieved by Directory.GetFiles() and 'paste' them else where (a glorified copy and paste/back up). I'm actually not too worried about system files, just 'user files'.

Upvotes: 3

Views: 3496

Answers (2)

Picrofo Software
Picrofo Software

Reputation: 5571

The directory %AppData% is a system-protected directory. Windows will try to block any access to this directory as soon as the access was not authorized (An access from another user than the Administrator).

Only the Administrator by default has privileges to read and write from/to this directory.

Alternatively, you can catch the exception and see if the result is Access Denied. Then, you may prompt the user to run as an Administrator to complete this step. Here's a simple example to prompt the user to run as Administrator

try
{
     var allFiles = Directory.GetFiles("C:\\Users\\Dave", "*.*", SearchOption.AllDirectories);
}
catch (Exception EX)
{
     if (EX.Message.ToLower().Contains("is denied."))
     {
          ProcessStartInfo proc = new ProcessStartInfo();
          proc.UseShellExecute = true;
          proc.WorkingDirectory = Environment.CurrentDirectory;
          proc.FileName = Application.ExecutablePath;
          proc.Verb = "runas"; //Required to run as Administrator
          try
          {

          Process.Start(proc);

          }
          catch
          {
               //The user refused to authorize
          }
     }
}

However, you may always prompt the user to authorize when your application launches which is NOT always RECOMMENDED. To do this, you'll have to edit your project app.manifest file

Locate and change the following line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

Thanks,
I hope you find this helpful :)

Upvotes: 3

snorifu
snorifu

Reputation: 49

It is better to use a foreach loop to get the folder names that you can acces:

DirectoryInfo dI = new DirectoryInfo(@"C:\Users\Dave");
List<string> files = new List<string>();
foreach (DirectoryInfo subDI in dI.GetDirectories())
{
        if ((subDI.Attributes & (FileAttributes.ReparsePoint | FileAttributes.System)) !=
        (FileAttributes)0)
              continue;
        files.Add(subDI.FullName);
}

Upvotes: 1

Related Questions