search in restricted access folders

2019-09-18 13:28发布

问题:

I want to list all files and folders that my program has access to and write them to a text file. i write a method which help me do this but in some cases (folder is access denied) my program stop working. i search here a lot and find some links that said use try/catch and sth else but i cant fix my problem yet.

string spcdirectorypath = @"C:\Users";
            string spcfiletape = "*.*";
            DirectoryInfo d = new DirectoryInfo(spcdirectorypath);//Assuming Test is your Folder
            FileInfo[] Files = d.GetFiles(spcfiletape, SearchOption.AllDirectories); //Getting Text files
            foreach (FileInfo file in Files)
                {
                    string str = file.FullName + "\n";
                    richTextBox1.AppendText(str);
                }

now how can i fix this problem? or in other hands can i access to that restricted folders? sorry for this repetitive Q and thanks for best answers :)?

回答1:

You should check th permissions before trying to list the files, if you want to avoid unauthorized access exceptions.

Here I adapt for your case the example of MSDN about walking a directory tree :

void YourMethod()
{
   string spcdirectorypath = @"C:\Users";
   DirectoryInfo d = new DirectoryInfo(spcdirectorypath);

   WalkDirectoryTree(d)
}

static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
    System.IO.FileInfo[] files = null;
    System.IO.DirectoryInfo[] subDirs = null;

    // First, process all the files directly under this folder 
    try
    {
        files = root.GetFiles("*.*");
    }
    // This is thrown if even one of the files requires permissions greater 
    // than the application provides. 
    catch (UnauthorizedAccessException e)
    {
        // You may decide to do something different here. For example, you 
        // can try to elevate your privileges and access the file again.
    }

    catch (System.IO.DirectoryNotFoundException e)
    {
        // You may decide to do something different here. For example, you 
        // can log soething.
    }

    if (files != null)
    {
        foreach (System.IO.FileInfo fi in files)
        {
            // In this example, we only access the existing FileInfo object. If we 
            // want to open, delete or modify the file, then 
            // a try-catch block is required here to handle the case 
            // where the file has been deleted since the call to TraverseTree().
            string str = fi.FullName + "\n";
            richTextBox1.AppendText(str);
        }

        // Now find all the subdirectories under this directory.
        subDirs = root.GetDirectories();

        foreach (System.IO.DirectoryInfo dirInfo in subDirs)
        {
            // Resursive call for each subdirectory.
            WalkDirectoryTree(dirInfo);
        }
    }            
}


回答2:

The process under which you are running your program should have the required privileges otherwise you wont be able access the folder and enumerate the files. In your test environment make sure you run this code as an administrator.



回答3:

Just skip those directories. If it fails, ignore:

try
{
  foreach (var file in Directory.GetFiles(spcdirectorypath))
  {
    richTextBox1.AppendText(file + "\r\n");
  }
}
catch (IOException)
{
  richTextBox1.AppendText("Failed " + spcdirectoryPath + "\r\n");
}

If the exception is not an IOException, just change it to the one that actually gets thrown.



回答4:

I guess that UpdatusUser is a user on that machine. User folders have restricted permissions. Try to run your application under UpdatusUser. If you don't run your application with UpdatusUser, you may try to set permission on UpdatusUser folder for your user (right-click on folder Properties/Security).

If you still encounter issues go deeper and see what is blocking using Process Monitor.