WIN7 中的 Getfiles() UnAuthorizedAccessAcception

发布于 2024-10-14 16:09:59 字数 1478 浏览 3 评论 0原文

问题

  1. UnAuthorizedAccessException:递归搜索目录(例如 C:\
    ) “对路径‘c:\Documents and Settings\’的访问被拒绝。”即使 UAC 权限已升级并且已升级,也会发生这种情况。管理员组访问权限。

尝试的方法

  1. 尝试& Catch:使用以下方法之一(Exception、UnAuthorizedAccessException、Blank Catch、继续)

问题

  1. 您如何处理这种异常并继续正常运行您的程序?这需要同时适用于非管理员和管理员帐户。

示例代码

using System;
using System.IO;

namespace filecheck
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int html = 0;
            try
            {
                string[] filePaths = Directory.GetFiles(@"c:\", "*.html", SearchOption.AllDirectories);

                foreach (string files in filePaths)
                {
                    if (Convert.ToBoolean(files.IndexOf("html")))
                    {
                        html++;
                    }
                    Console.WriteLine(files);
                    i++;

                }
                Console.Write("# Files found: {0} Html: {1)", i, html);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());

            }

        }
    }
}

Problems

  1. UnAuthorizedAccessException: When searching a directory recursively such as C:\
    A "Access to the path 'c:\Documents and Settings\' is denied." Occurs even with UAC Priveledges upgraded & Administrator group access.

Attempted Methods

  1. Try & Catch: Using either one of these methods(Exception, UnAuthorizedAccessException, Blank Catch, continue)

Questions

  1. How do you handle this kind of exception and continue running your program as normal? This needs to work both on non-admin and administrator accounts.

Example Code

using System;
using System.IO;

namespace filecheck
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int html = 0;
            try
            {
                string[] filePaths = Directory.GetFiles(@"c:\", "*.html", SearchOption.AllDirectories);

                foreach (string files in filePaths)
                {
                    if (Convert.ToBoolean(files.IndexOf("html")))
                    {
                        html++;
                    }
                    Console.WriteLine(files);
                    i++;

                }
                Console.Write("# Files found: {0} Html: {1)", i, html);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());

            }

        }
    }
}

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

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

发布评论

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

评论(2

冷月断魂刀 2024-10-21 16:09:59

不幸的是,处理这个问题的唯一方法是手动执行递归。即使在微软自己的示例代码中,他们也是这样做的,只是为了避免整个搜索失败,因为无法访问一个或多个目录。

换句话说,只有当您搜索有限的目录子集(您确定不包含任何您无权访问的目录)时,才使用 SearchOption.AllDirectories

Unfortunately the only way to handle this is by doing the recursion manually. Even in Microsoft's own sample code they do it this way, just to avoid that the whole search fails because one or more directories can not be accessed.

So in other words, only use SearchOption.AllDirectories when you're searching a limited subset of directories which you're certain won't contain any directories which you won't have access to.

饮惑 2024-10-21 16:09:59

要让您的程序同时与管理员和非管理员用户一起工作,您需要模拟用户或重新构建应用程序,以便在每次由任何用户执行或使用时“以管理员身份运行”。要构建这种类型的应用程序,您需要将 app.manifest 文件添加到您的项目中,并取消注释 app.manifest 中的以下设置行。

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

有关更多信息,请阅读此处: http://midnightprogrammer.net/post/How-To-Build-UAC-Compatible-Application-In-NET。 ASPX

To get your program working with both admin and non-admin users you either need to impersonate the user or re-build your application to "Run as Administrator" every time it is being executed or used by any user. To build this kind of application you need to add app.manifest file to your project and un-comment the following line of setting in app.manifest

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

For more read here: http://midnightprogrammer.net/post/How-To-Build-UAC-Compatible-Application-In-NET.aspx

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