简体   繁体   中英

System.UnauthorizedAccessException when getting files

I've asked a very similar question before here . But that was about getting directories, and this is about files. And these codes are a bit different from each other. Ever since I've been trying to convert this to make it look like the answer on my old question, I haven't been able to make it work.

string[] files = Directory.GetFiles(ScanPath, "*.*", System.IO.SearchOption.AllDirectories);
DateTime From = DateTime.Now.AddHours(-24);
DateTime To = DateTime.Now;

foreach (string name in files)
{
   FileInfo file = new FileInfo(name);
   string fullname = file.FullName;

   if (file.LastWriteTime >= From & file.LastWriteTime <= To && file.Length >= ScanSize)
      Console.WriteLine(file.FullName + " ; " + "last changed at  " + " ; " + file.LastWriteTime.ToString());
}

I've been getting the same errors as I explained in the other question. Because I don't know where to put the code of the foreach in a recursion. Since it's not an enumeration but a Directory.GetFiles() .

The error occurs with:

Directory.GetFiles(ScanPath, "*", SearchOption.AllDirectories);

because this gets all the files of the directories at once. But if I remove it, it only gets the files in the given path, without any of the files in the subdirectories. So I was told to apply recursion.

I am the administrator of the system and I plan to run this on the entire data drive. D:\\

I'm hoping anyone here knows a good example.

Your app could not have access rights to some folders, for others you can use the following code:

void DiscoverDirs(string where, List<string> files, Func<FileInfo, bool> filter)
{
    try
    {
        var di = new DirectoryInfo(where);
        files.AddRange(di.EnumerateFiles().Where(filter).Select(x => x.FullName));

        foreach (var dir in Directory.GetDirectories(where))
        {
            DiscoverDirs(dir, files, filter);
        }
    }
    catch
    {
        // no access fo this dir, ignore
    }
}

Usage:

DateTime From = DateTime.Now.AddHours(-24);
DateTime To = DateTime.Now;
var ScanSize = 5*1024*1024;

var list = new List<string>();
DiscoverDirs(@"C:\", list, 
    file => file.LastWriteTime >= From & file.LastWriteTime <= To && file.Length >= ScanSize);

foreach (string name in list)
{
    FileInfo file = new FileInfo(name);
    string fullname = file.FullName;

    Console.WriteLine(file.FullName + " ; " + "last changed at  " + " ; " + file.LastWriteTime.ToString());
}

You might be getting "UnauthorizedAccessException" while accessing the some of the system directories.List of the directory causing the problems are directories which are actually just redirection to other directory. May be you can tried out the following code if it helps-

try
{
    foreach (String file in Directory.GetFiles(directoryName, pattern, SearchOption.TopDirectoryOnly))
    {
       // do stuff
    }
catch (UnauthorizedAccessException uae)
{
           //handle
}
catch (Exception e) 
{ 
           //handle
}

Alternative:

string[] directories = Directory.GetDirectories(ScanPath);

    foreach (string directory in directories)
    {
        string[] filesinCurrentDirectory = Directory.GetFiles(directory, "*.*", System.IO.SearchOption.AllDirectories);
        foreach (string file in filesinCurrentDirectory)
        {
            MessageBox.Show(file);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM