访问 C# 中拒绝的目录 - 控制台应用程序
Access to the directory denied in C# - Console Application
我试图访问 Documents 目录和子目录,但每次都提示访问被拒绝。我看到异常:
System.UnauthorizedAccessException: Access to the path 'C:\Users\MyUser\Documents\My Music' is denied
这是我的代码 - 我想要做的就是获取该目录的总大小。
class Program
{
static void Main(string[] args)
{
try {
// Make a reference to a directory.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo di = new DirectoryInfo(path);
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles(".", SearchOption.AllDirectories);
// Display the names and sizes of the files.
Console.WriteLine("The directory {0} contains the following files:", di.Name);
long size = 0;
foreach (FileInfo f in fiArr)
{
size += f.Length;
size++;
}
Console.WriteLine("The size of desktop files." + size);
}
catch(Exception e)
{
Console.WriteLine("Exceptions {0}" , e);
}
}
}
搜索应该在 TopDirectoryOnly 而不是 AllDirectories。
问题出在这一行:
FileInfo[] fiArr = di.GetFiles(".", SearchOption.AllDirectories);
改为:
FileInfo[] fiArr = di.GetFiles(".", SearchOption.TopDirectoryOnly);
这应该有效。
某些 SpecialFolders 需要特定的管理员权限才能 运行,无一例外。您必须 运行 您的管理员权限代码。
根据我浏览 this thread 可以收集到的信息,这些文件夹可能是为 Windows 向后兼容而提供的软链接。
为了解决编码问题,您可以创建自己的递归文件夹搜索,忽略当前用户无权访问给定文件夹时抛出的异常。
也许是这样的:
static IEnumerable<FileInfo> GetAllFilesRecursive(string path)
{
var di = new DirectoryInfo(path);
var files = new List<FileInfo>();
files.AddRange(di.GetFiles("."));
foreach (var directory in Directory.GetDirectories(path))
{
try
{
files.AddRange(GetAllFilesRecursive(directory));
}
catch (UnauthorizedAccessException) // ignore directories which the user does not have access to
{}
}
return files;
}
然后重写代码以使用新函数:
static void Main(string[] args)
{
try
{
// Make a reference to a directory.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fiArr = GetAllFilesRecursive(path);
// Display the names and sizes of the files.
Console.WriteLine("The directory {0} contains the following files:", path);
long size = 0;
foreach (FileInfo f in fiArr)
{
size += f.Length;
size++;
}
Console.WriteLine("The size of desktop files." + size);
}
catch (Exception e)
{
Console.WriteLine("Exceptions {0}", e);
}
}
我试图访问 Documents 目录和子目录,但每次都提示访问被拒绝。我看到异常:
System.UnauthorizedAccessException: Access to the path 'C:\Users\MyUser\Documents\My Music' is denied
这是我的代码 - 我想要做的就是获取该目录的总大小。
class Program
{
static void Main(string[] args)
{
try {
// Make a reference to a directory.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo di = new DirectoryInfo(path);
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles(".", SearchOption.AllDirectories);
// Display the names and sizes of the files.
Console.WriteLine("The directory {0} contains the following files:", di.Name);
long size = 0;
foreach (FileInfo f in fiArr)
{
size += f.Length;
size++;
}
Console.WriteLine("The size of desktop files." + size);
}
catch(Exception e)
{
Console.WriteLine("Exceptions {0}" , e);
}
}
}
搜索应该在 TopDirectoryOnly 而不是 AllDirectories。
问题出在这一行:
FileInfo[] fiArr = di.GetFiles(".", SearchOption.AllDirectories);
改为:
FileInfo[] fiArr = di.GetFiles(".", SearchOption.TopDirectoryOnly);
这应该有效。
某些 SpecialFolders 需要特定的管理员权限才能 运行,无一例外。您必须 运行 您的管理员权限代码。
根据我浏览 this thread 可以收集到的信息,这些文件夹可能是为 Windows 向后兼容而提供的软链接。
为了解决编码问题,您可以创建自己的递归文件夹搜索,忽略当前用户无权访问给定文件夹时抛出的异常。
也许是这样的:
static IEnumerable<FileInfo> GetAllFilesRecursive(string path)
{
var di = new DirectoryInfo(path);
var files = new List<FileInfo>();
files.AddRange(di.GetFiles("."));
foreach (var directory in Directory.GetDirectories(path))
{
try
{
files.AddRange(GetAllFilesRecursive(directory));
}
catch (UnauthorizedAccessException) // ignore directories which the user does not have access to
{}
}
return files;
}
然后重写代码以使用新函数:
static void Main(string[] args)
{
try
{
// Make a reference to a directory.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fiArr = GetAllFilesRecursive(path);
// Display the names and sizes of the files.
Console.WriteLine("The directory {0} contains the following files:", path);
long size = 0;
foreach (FileInfo f in fiArr)
{
size += f.Length;
size++;
}
Console.WriteLine("The size of desktop files." + size);
}
catch (Exception e)
{
Console.WriteLine("Exceptions {0}", e);
}
}