Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查应用程序是否可以访问目录?_C#_.net - Fatal编程技术网

C# 如何检查应用程序是否可以访问目录?

C# 如何检查应用程序是否可以访问目录?,c#,.net,C#,.net,在我的应用程序中,我需要检查我是否有写入文件夹的权限。我使用以下方法: public bool IsAvailable(string path) { bool hasPermissions = false; if (Directory.Exists(path)) { var permission = new FileIOPermission(FileIOPermissionA

在我的应用程序中,我需要检查我是否有写入文件夹的权限。我使用以下方法:

public bool IsAvailable(string path)
        {
            bool hasPermissions = false;

            if (Directory.Exists(path))
            {
                var permission = new FileIOPermission(FileIOPermissionAccess.Write, path);
                try
                {
                    permission.Demand();
                    hasPermissions = true;
                }
                catch(SecurityException e)
                {
                    hasPermissions = false;
                }
            }

            return hasPermissions;
        }
当我给它一个我知道没有人可以访问的文件夹路径时(我在文件夹属性的“安全”选项卡中删除了所有用户的所有权限),它不会引发任何异常。它只是沿着try块继续

你知道为什么或者如何更好地进行这项检查吗

我在其他问题上找到的AppDomain.PermissionSet属性相关答案没有成功

提前感谢。

此方法(询问是否可访问,然后采取措施)容易受到比赛条件的影响。在您的检查和对该目录中内容的实际访问之间,权限可能会更改

最好尝试在该目录中读/写一些东西,并捕获一个潜在的异常

所以不要

if(IsAvailable(path)) {
    try {
        doSomething();
    } catch (...) {
    }
}
而是

try {
    doSomething();
} catch (...) {
}

格蕾丝·霍珀引述:

“请求原谅总是比获得许可容易。”


我使用了以下方法来完成它:

public static bool HasWritePermissionOnDir(string path)
    {
        var writeAllow = false;
        var writeDeny = false;
        var accessControlList = Directory.GetAccessControl(path);
        if (accessControlList == null)
            return false;
        var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
        if (accessRules == null)
            return false;

        foreach (FileSystemAccessRule rule in accessRules)
        {
            if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;

            if (rule.AccessControlType == AccessControlType.Allow)
                writeAllow = true;
            else if (rule.AccessControlType == AccessControlType.Deny)
                writeDeny = true;
        }

        return writeAllow && !writeDeny;
    }

请让我知道它是否对您有帮助,如果是,请也标记它

您正在使用的代码访问安全构造是在.NET运行时级别强制执行的,并且是更高级别的,完全独立于操作系统用于访问控制的安全描述符。这就是说,phresnel回答的要点是正确的——当您开始尝试某些I/O时,您执行的任何检查都可能过期,因此更好的方法是尝试I/O,并做好准备,以防由于权限问题而失败。或者我可以尝试将临时文件写入该文件夹,如果失败,返回false,否则返回true并删除临时文件…是的,就像你在“为什么或者如何更好地执行此检查?”中要求的那样:)这很有效。。。现在要理解代码。。。我知道比赛条件可能会发生,但这是有帮助的代码。谢谢。您好,您能告诉我如何为路径列表编写上述代码吗?我的意思是在参数中传递路径列表,而不是只传递一条路径。@Neel:您的意思是:列表路径您总是可以将程序编写为public bool IsAvailable(列表路径),并使用foreach处理所有路径。。我不是AnjaliI拒绝使用“s-1-5-11”的经过身份验证的用户对文件夹的访问,但是我得到了针对这个特定SID的AccessContolType Allow和deny。有人知道为什么吗?为什么我会被允许?也许你可以养成在回答中提供解释的习惯。这也将为您赢得更多的选票;)
public static bool HasWritePermissionOnDir(string path)
    {
        var writeAllow = false;
        var writeDeny = false;
        var accessControlList = Directory.GetAccessControl(path);
        if (accessControlList == null)
            return false;
        var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
        if (accessRules == null)
            return false;

        foreach (FileSystemAccessRule rule in accessRules)
        {
            if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;

            if (rule.AccessControlType == AccessControlType.Allow)
                writeAllow = true;
            else if (rule.AccessControlType == AccessControlType.Deny)
                writeDeny = true;
        }

        return writeAllow && !writeDeny;
    }