计算目录大小时获取UnauthorizedAccessException [英] Getting UnauthorizedAccessException while counting the size of the directory

查看:31
本文介绍了计算目录大小时获取UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法,可以计算目录及其中所有文件的大小.这是代码:

I have a simple method that counts the size of the directory and all files within it. Here is the code:

using System;
using System.IO;

namespace MyProject.Libs
{
    public sealed class DirectorySize
    {
        public static long GetDirectorySize(DirectoryInfo dir)
        {
            long total = 0;

            FileInfo[] fileInfos = dir.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
            {
                total += fileInfo.Length;
            }

            DirectoryInfo[] dirInfos = dir.GetDirectories();
            foreach (DirectoryInfo dirInfo in dirInfos)
            {
                total += DirectorySize.GetDirectorySize(dirInfo);
            }

            return total;
        }
    }
}

当我在驱动器c:\上使用它时,出现"UnauthorizedAccessException"消息,并提示对路径'C:\ Documents and Settings'的访问被拒绝."那就是:

When I use it on drive c:\ I get "UnauthorizedAccessException" with a message "Access to the path 'C:\Documents and Settings' is denied." That is:

DirectoryInfo di = new DirectoryInfo(Path.GetPathRoot(Environment.SystemDirectory));
long ds = DirectorySize.GetDirectorySize(di);

试图以管理员身份运行Visual Studio.全都一样.为什么?

Tried to run Visual Studio as Administrator. All the same. Why?

推荐答案

您的代码在 C:\ Documents and Settings 上失败,该代码现在为

Your code fails on C:\Documents and Settings which is now a junction point that points to C:\Users. You could check that with the FileAttributes.ReparsePoint of the directory.

这是经过修改的代码,具有附加的异常处理功能(用于其他未经您授权的目录):

Here's the modified code with additional exception handling(for other dirs which you are not authorized):

public sealed class DirectorySize
{
    public static long GetDirectorySize(DirectoryInfo dir)
    {
        long total = 0;
        FileAttributes attributes = File.GetAttributes(dir.FullName);
        if (!((attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint))
        {
            try{
                FileInfo[] fileInfos = dir.GetFiles();
                foreach (FileInfo fileInfo in fileInfos)
                {
                    total += fileInfo.Length;
                }

                DirectoryInfo[] dirInfos = dir.GetDirectories();
                foreach (DirectoryInfo dirInfo in dirInfos)
                {
                    total += DirectorySize.GetDirectorySize(dirInfo);
                }
            } catch (UnauthorizedAccessException)
            { 
                // log this?
            }
        }

        return total;
    }
}

连接点数(在Windows中)

Junction Points (Windows)

在Windows Vista和Windows Server 2008中,默认位置为用户数据和系统数据已更改.例如,用户数据以前存储在%SystemDrive%\ Documents and Settings中目录现在存储在%SystemDrive%\ Users目录中.为了向后兼容,旧位置具有连接点指向新位置.例如, C:\ Documents and Settings 是现在是指向 C:\ Users 的连接点.备份应用程序必须能够备份和还原连接点.这些路口这些点可以识别如下: FILE_ATTRIBUTE_REPARSE_POINT ,FILE_ATTRIBUTE_HIDDEN和FILE_ATTRIBUTE_SYSTEM文件属性集.他们也有访问权限控制列表(ACL)设置为拒绝所有人的读取权限.应用领域调用特定路径可以遍历这些交叉点,如果他们具有所需的权限.但是,试图枚举连接点的内容将导致故障.

In Windows Vista and Windows Server 2008, the default locations for user data and system data have changed. For example, user data that was previously stored in the %SystemDrive%\Documents and Settings directory is now stored in the %SystemDrive%\Users directory. For backward compatibility, the old locations have junction points that point to the new locations. For example, C:\Documents and Settings is now a junction point that points to C:\Users. Backup applications must be capable of backing up and restoring junction points. These junction points can be identified as follows: They have the FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_HIDDEN, and FILE_ATTRIBUTE_SYSTEM file attributes set. They also have their access control lists (ACLs) set to deny read access to everyone. Applications that call out a specific path can traverse these junction points if they have the required permissions. However, attempts to enumerate the contents of the junction points will result in failures.

这篇关于计算目录大小时获取UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆