Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# 如何检查Windows文件是否可读/写?_C#_.net_Permissions_Acl_File Permissions - Fatal编程技术网

C# 如何检查Windows文件是否可读/写?

C# 如何检查Windows文件是否可读/写?,c#,.net,permissions,acl,file-permissions,C#,.net,Permissions,Acl,File Permissions,首先:我知道这对于检查我是否能写是不可靠的。我正在编写一个文件传输客户端,希望在“远程”和“本地”文件浏览器窗格之间实现功能奇偶性。我完全理解,无论执行何种操作,我都必须处理任何与权限相关的异常;这不是编程检查,只是向用户显示 我已经看到了一些例子,但我尝试过的每一件事都不是不可理解就是不起作用。我尝试了以下两种方法,但对于我绝对无法写入的内容(例如C:\Windows或C:\Program文件的内容),这两种方法都只返回“是”: 及 (同样,我意识到捕获异常是可怕的,而使用try/catch作

首先:我知道这对于检查我是否能写是不可靠的。我正在编写一个文件传输客户端,希望在“远程”和“本地”文件浏览器窗格之间实现功能奇偶性。我完全理解,无论执行何种操作,我都必须处理任何与权限相关的异常;这不是编程检查,只是向用户显示

我已经看到了一些例子,但我尝试过的每一件事都不是不可理解就是不起作用。我尝试了以下两种方法,但对于我绝对无法写入的内容(例如C:\Windows或C:\Program文件的内容),这两种方法都只返回“是”:

(同样,我意识到捕获
异常
是可怕的,而使用try/catch作为逻辑则不那么可怕,我只是想让它正常工作)

第一个问题告诉我,
isgrated
已被弃用,我应该使用
AppDomain.PermissionSet
Application.PermissionSet
,但我找不到任何有意义的解释来说明如何使用它们。我还看到,我应该手动枚举所有ACL,以自己解决问题,但同样没有这方面的实际示例。有很多设置权限的例子,但很少有检查权限的例子


任何帮助都将不胜感激。

好吧,以下是我最终得到的:

    private readonly static WindowsIdentity _identity = WindowsIdentity.GetCurrent();
    protected static bool GetPermission(FileSystemRights right, string path)
    {
        FileSecurity fs;
        try
        {
            fs = System.IO.File.GetAccessControl(path);
        }
        catch(InvalidOperationException)
        {
            // called on a disk that's not present, ...
            return false;
        }
        catch(UnauthorizedAccessException)
        {
            return false;
        }
        foreach(FileSystemAccessRule fsar in fs.GetAccessRules(true, true, typeof(SecurityIdentifier)))
        {
            if(fsar.IdentityReference == _identity.User && fsar.FileSystemRights.HasFlag(right) && fsar.AccessControlType == AccessControlType.Allow)
            {
                return true;
            }
            else if(_identity.Groups.Contains(fsar.IdentityReference) && fsar.FileSystemRights.HasFlag(right) && fsar.AccessControlType == AccessControlType.Allow)
            {
                return true;
            }
        }
        return false;
    }
当然,这并不理想,因为它忽视了否认权利。但是我不知道在哪种情况下应用各种ACE(我想?)来找出“正确”的能力。尽管如此,拒绝规则还是比较少见的,所以它在大多数情况下对我有效

System.Security.Permissions.FileIOPermission fp = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, element.Path);
try
{
    fp.Assert();
    return true;
}
catch(Exception x)
{
    return false;
}
    private readonly static WindowsIdentity _identity = WindowsIdentity.GetCurrent();
    protected static bool GetPermission(FileSystemRights right, string path)
    {
        FileSecurity fs;
        try
        {
            fs = System.IO.File.GetAccessControl(path);
        }
        catch(InvalidOperationException)
        {
            // called on a disk that's not present, ...
            return false;
        }
        catch(UnauthorizedAccessException)
        {
            return false;
        }
        foreach(FileSystemAccessRule fsar in fs.GetAccessRules(true, true, typeof(SecurityIdentifier)))
        {
            if(fsar.IdentityReference == _identity.User && fsar.FileSystemRights.HasFlag(right) && fsar.AccessControlType == AccessControlType.Allow)
            {
                return true;
            }
            else if(_identity.Groups.Contains(fsar.IdentityReference) && fsar.FileSystemRights.HasFlag(right) && fsar.AccessControlType == AccessControlType.Allow)
            {
                return true;
            }
        }
        return false;
    }