C#: Access to path <'filepath'> is denied [duplicate]

别等时光非礼了梦想. 提交于 2019-12-24 00:58:00

问题


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


回答1:


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



回答2:


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\



来源:https://stackoverflow.com/questions/9228426/c-access-to-path-filepath-is-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!