2

Possible Duplicate:
Why am I getting an access denied error for the Documents and Settings folder?

I have a program that will read all .exe files from a user specified folder and its sub-directories. While testing I tried to scan using C:\ as root.

This threw out an "UnauthorizedAccessException was unhandled" {"Access to the path 'c:\Documents and Settings\' is denied."}

The code snippet doing this is as follows:

 string customScanFolder = Console.ReadLine();
    Console.WriteLine("");

    if (Directory.Exists(customScanFolder) == true)
    {
        string[] customScanResults = Directory.GetFiles(customScanFolder, "*.EXE", SearchOption.AllDirectories);

I am still very much a C# novice, but all Googling and related topics on here have not helped point me in the right direction.

The system my program is running on is Win7 with UAC disabled. The .exe has 'Run as Administrator' enabled.

The program will only ever read from files, not write to them. What do I need to do to either grant access rights or avoid this error another way?

Thanks,

Ben

Community
  • 1
  • 1
  • 2
    dup - http://stackoverflow.com/questions/4814112/getfiles-unauthorizedaccessacception-in-win7 and http://stackoverflow.com/questions/8529806/why-am-i-getting-an-access-denied-error-for-the-documents-and-settings-folder – Zach Green Feb 10 '12 at 13:25
  • You have C:\Documents and Settings\ on a win7 machine? – Stealth Rabbi Feb 10 '12 at 13:26
  • Thanks, will take a look :) Yes, I meant C:\Users –  Feb 10 '12 at 14:30

2 Answers2

3

C:\Documents and Settings is not a folder in Windows 7. It's a Junction (or link) to C:\Users. You should be ignoring it.

C:\>dir /a:s C:
 Volume in drive C has no label.
 Volume Serial Number is 86F5-8CF5

 Directory of C:\

03/04/2011  10:19 AM    <DIR>          $Recycle.Bin
02/03/2012  03:00 PM    <DIR>          Config.Msi
07/14/2009  12:08 AM    <JUNCTION>     Documents and Settings [C:\Users]
12/30/2011  01:19 PM    12,882,337,792 pagefile.sys
10/23/2009  03:07 PM    <DIR>          Recovery
01/11/2011  10:24 AM    <DIR>          System Volume Information
               1 File(s) 12,882,337,792 bytes
               5 Dir(s)  64,772,997,120 bytes free
Nasir
  • 10,935
  • 8
  • 31
  • 39
  • You're right, that was a typing before I think. Visual Studio 2010 said 'Docs and settings' when refering to C:\users\ –  Feb 10 '12 at 14:30
0

This is the same as an issue I encountered recently. See this post for the solution I used.

Windows service running as system cannot access C:\users\

Community
  • 1
  • 1
Grant H.
  • 3,689
  • 2
  • 35
  • 53