7

I'm using a simple DirectoryInfo to grab all directories on the root on the C drive. However, I'm running under administrator and i'm getting the error of path access denied, below is the code that I am running. How do I resolve the issue of path access?

DirectoryInfo Dinfo = new DirectoryInfo(@"C:\");
DirectoryInfo[] directories = Dinfo.GetDirectories("*.*", SearchOption.AllDirectories);
Omar
  • 16,329
  • 10
  • 48
  • 66
  • You are running as _administrator_, but is your process _elevated_? – K-ballo Jun 09 '12 at 19:26
  • My guess is no and i'm not sure how to do that –  Jun 09 '12 at 19:27
  • 1
    possible duplicate of [Why am I getting an access denied error for the Documents and Settings folder?](http://stackoverflow.com/questions/8529806/why-am-i-getting-an-access-denied-error-for-the-documents-and-settings-folder) – Damith Jun 09 '12 at 19:38
  • @Damith Having no answer or a way around is not reasonable, I should be able to as an administrator be able to see all documents and folders under a users path –  Jun 09 '12 at 19:47
  • Do what you do in the explorer when you are denied access to a folder. Take recursive ownership of all folders and then try – Sandeep Singh Rawat Jun 09 '12 at 20:48

4 Answers4

4

On newer versions of Windows C:\Document and Settings is a junction point, kind of a file system shortcut. It is not a normal directory, which means that it doesn't really work as a normal directory.

If you type in C:\Document and Settings in the start->run box you will also get an access denied error, so it is nothing specific to your program.

I'm a bit confused by how this works however. I thought that the junction point would be a transparent link to the new location which is c:\users but obviously not.

Edit

After looking at the duplicate question I'm less confused. The junction point really links to the new location which is c:\users. However, there is an explicit deny acl for reading on the junction point to prevent anyone from using it to read things:

C:>cacls "Documents and Settings" C:\Documents and Settings Everyone:(DENY)(special access:)

                               FILE_READ_DATA

                      Everyone:R
                      NT AUTHORITY\SYSTEM:F
                      BUILTIN\Administrators:F

C:>

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • As I do understand you're point, should I not be able to see all the user folders underneath of Docs and settings? –  Jun 09 '12 at 19:28
  • See my update, and the duplicate question. There is an explicit deny acl on the junction point which prevents reading. – Anders Abel Jun 09 '12 at 19:46
  • So how do I know what all users and user folders programmatically? –  Jun 09 '12 at 19:50
  • 1
    Anders - C:\users\All Users\Application Data\ access denied –  Jun 09 '12 at 20:03
  • 1
    `c:\users\all users\application data` is also a junction point, pointing to `c:\programdata`, with a deny access just like what I showed above. To check yourself, use `dir /al` and `cacls`. – Anders Abel Jun 09 '12 at 21:05
1

If you are running as Administrator you might still run into

  • UAC being active. Fix by using "run as..."
  • Folders that you can only access when you explicitly take ownership.
Emond
  • 50,210
  • 11
  • 84
  • 115
1

C:\Document and Settings is a junction point and additionnally You cannot access the System Volume Information directory.which is placed on C:\ root you have to trap any security exceptions and skip it to work your code.

Raab
  • 34,778
  • 4
  • 50
  • 65
  • 2
    how do you do a try catch on the above code WITHOUT breaking its iteration? –  Jun 09 '12 at 19:33
  • How does the `System Volume Information` directory relate to this? The question is about `Documents and Settings` being inaccessible. – Anders Abel Jun 09 '12 at 19:44
-1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Solution
{
    public class Program
    {
 static void Main(string[] args)
        {
            DirSearch(@"YOUR PATH");
            Console.ReadKey();
        }

        static void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
Mayur Narula
  • 150
  • 1
  • 4
  • 1
    this is a generic answer for reading files, directories, does not relate to the specific question of Access Denied; even with this answer, the access will still be denied ! – joedotnot Mar 13 '21 at 13:11