WIN7 中的 Getfiles() UnAuthorizedAccessAcception
问题
- UnAuthorizedAccessException:递归搜索目录(例如 C:\
) “对路径‘c:\Documents and Settings\’的访问被拒绝。”即使 UAC 权限已升级并且已升级,也会发生这种情况。管理员组访问权限。
尝试的方法
- 尝试& Catch:使用以下方法之一(Exception、UnAuthorizedAccessException、Blank Catch、继续)
问题
- 您如何处理这种异常并继续正常运行您的程序?这需要同时适用于非管理员和管理员帐户。
示例代码
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
- 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
- Try & Catch: Using either one of these methods(Exception, UnAuthorizedAccessException, Blank Catch, continue)
Questions
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,处理这个问题的唯一方法是手动执行递归。即使在微软自己的示例代码中,他们也是这样做的,只是为了避免整个搜索失败,因为无法访问一个或多个目录。
换句话说,只有当您搜索有限的目录子集(您确定不包含任何您无权访问的目录)时,才使用
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.要让您的程序同时与管理员和非管理员用户一起工作,您需要模拟用户或重新构建应用程序,以便在每次由任何用户执行或使用时“以管理员身份运行”。要构建这种类型的应用程序,您需要将 app.manifest 文件添加到您的项目中,并取消注释 app.manifest 中的以下设置行。
有关更多信息,请阅读此处: 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
For more read here: http://midnightprogrammer.net/post/How-To-Build-UAC-Compatible-Application-In-NET.aspx