C# Getfiles()在WIN7中未经授权的访问接受
问题C# Getfiles()在WIN7中未经授权的访问接受,c#,exception,windows-7,unauthorized,C#,Exception,Windows 7,Unauthorized,问题 UnAuthorizedAccessException:递归搜索目录(如C:\ “对路径“c:\Documents and Settings\”的访问被拒绝。”即使在UAC权限升级和管理员组访问的情况下也会发生此情况。 尝试的方法 using System; using System.IO; namespace filecheck { class Program { static void Main(string[] args) {
“对路径“c:\Documents and Settings\”的访问被拒绝。”即使在UAC权限升级和管理员组访问的情况下也会发生此情况。
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());
}
}
}
}
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());
}
}
}
}
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());
}
}
}
}
不幸的是,处理这个问题的唯一方法是手动执行递归。即使在微软自己的网站上,他们也是这样做的,只是为了避免因为一个或多个目录无法访问而导致整个搜索失败
换句话说,仅使用
SearchOption。当您搜索有限的目录子集时,如果您确定其中不包含任何您无权访问的目录,请使用AllDirectories
。要使您的程序同时与管理员和非管理员用户一起工作,您需要模拟用户或重新构建应用程序每次由任何用户执行或使用时都要“以管理员身份运行”。要生成此类应用程序,您需要将app.manifest文件添加到项目中,并取消对app.manifest中以下设置行的注释
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
有关更多信息,请阅读此处:据我所知,不允许通过编程方式访问文档和设置。即使superadmin@Theun:什么是“超级管理员”?@Theun:我想你说的是隐藏管理员(内置)帐户。我没有尝试过,但这会让你不受限制地访问系统。我不需要。只需搜索一种方法,以避免我的搜索被这个讨厌的未授权访问阻止。这是不适用的,因为我是管理员角色,并且使用UAC升级特权。因此,您可以看到设置此选项当我启动我的程序时,仅使UAC提示我进行用户升级确认。即使在“C:\“它根本不返回任何内容。如果我将示例目录搜索更改为c:\user\\Desktop,它也会找到我要求的所有内容。我如何知道跳过哪个目录?如果当前用户角色没有权限,是否必须检查文件夹安全属性并跳过它们?必须有一个可能的解决办法。他们说你可以处理异常并继续,但这一次完全停止了你的程序的运行,这让我感到困惑。@Nightforce2:在Windows 7(可能还有Vista)中,系统驱动器的根(通常是C:\)在默认情况下是写保护的,但是,由于您的代码只读取数据,我会认为它不会因此而失败,所以我不确定您为什么会遇到这个问题。如果您的帐户没有对系统驱动器根目录的读取权限,我认为它将无法实际运行Windows…我认为这是因为UAC。它带来的问题比解决方案多。