C#:访问路径<“文件路径”>被拒绝

发布于 2025-01-04 17:06:42 字数 906 浏览 4 评论 0 原文

可能的重复:
为什么我会收到“文档和设置”文件夹出现访问被拒绝错误?

我有一个程序可以从用户指定的文件夹及其子目录中读取所有 .exe 文件。在测试时,我尝试使用 C:\ 作为 root 进行扫描。

这抛出了一个“UnauthorizedAccessException was unhandled” {“Access to the path 'c:\Documents and Settings\' is returned.”}

执行此操作的代码片段如下:

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

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

我仍然是一个 C# 新手,但所有 Google 搜索这里的相关主题并没有帮助我指明正确的方向。

我的程序运行的系统是Win7,禁用了UAC。 .exe 已启用“以管理员身份运行”。

该程序只会从文件中读取,而不会写入文件。我需要做什么才能授予访问权限或以其他方式避免此错误?

谢谢,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2025-01-11 17:06:42

C:\Documents and Settings 不是 Windows 7 中的文件夹。它是 C:\Users 的连接(或链接)。你应该忽略它。

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

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
咽泪装欢 2025-01-11 17:06:42

这和我最近遇到的一个问题是一样的。请参阅这篇文章了解我使用的解决方案。

作为系统运行的 Windows 服务无法访问 C:\users\

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\

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文