我的 C# 应用程序如何测试用户是否对网络共享具有“读取”访问权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/753120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:49:57  来源:igfitidea点击:

How can my C# app test whether the user has "Read" access to a network share?

c#windowsaclnovell

提问by flipdoubt

I work on a thick-client app that often runs into "issues" accessing network shares. Before doing any IO with the server, my app tests whether the share (usually of the form \\server\share$) exists. This works fine for detecting those scenarios in which the client has lost its connection to the server, but there are still those odd scenarios where the hidden share exists but the user does not have the rights to read from the within the share. Can someone share (no pun intended) the C# code required to test whether the current user can read files on a share? Should I be querying the share's ACL or the files within the share? What if the share is empty? What if the user is a local non-admin in a mixed environment (XP Pro workstation, Windows 2003 server without a domain on a Novell network)?

我在开发一个胖客户端应用程序,该应用程序在访问网络共享时经常遇到“问题”。在对服务器执行任何 IO 之前,我的应用程序会测试共享(通常采用 \\server\share$ 形式)是否存在。这对于检测客户端失去与服务器的连接的情况很有效,但仍然存在隐藏共享存在但用户无权从共享中读取的奇怪情况。有人可以共享(无双关语)测试当前用户是否可以读取共享文件所需的 C# 代码吗?我应该查询共享的 ACL 还是共享中的文件?如果共享是空的怎么办?如果用户是混合环境中的本地非管理员(XP Pro 工作站、Novell 网络上没有域的 Windows 2003 服务器)怎么办?

采纳答案by Erich Mirabal

The easiest way is to just do it (i.e. try to read a file, for example). As Jared mentioned, there is no way to make sure that you will be able to read in the future (network failure, change of permissions, etc).

最简单的方法就是这样做(例如,尝试读取文件)。正如 Jared 所提到的,没有办法确保您将来能够阅读(网络故障、权限更改等)。

As far as code goes, you could use the DirectoryInfoclass for some attempts at an answer:

就代码而言,您可以使用DirectoryInfo类尝试一些答案:

        string remotePath = @"\server\share$";
        bool haveAccess = false;

        DirectoryInfo di = new DirectoryInfo(remotePath);
        if (di.Exists)
        {
            try
            {
                // you could also call GetDirectories or GetFiles
                // to test them individually
                // this will throw an exception if you don't have 
                // rights to the directory, though
                var acl = di.GetAccessControl();
                haveAccess = true;
            }
            catch (UnauthorizedAccessException uae)
            {
                if (uae.Message.Contains("read-only"))
                {
                    // seems like it is just read-only
                    haveAccess = true;
                }
                // no access, sorry
                // do something else...
            }
        }

There are many shortcomings in the above code (such as the hard-coded "read-only" test), but it is just an example used to illustrate what you could do. DirectoryInfo has a few other helper methods that you can use to list the files in the folder. If you don't have access, the methods will throw an UnauthorizedAccessExceptionexception which you can use to test why the access failed. Check out the info on GetAccessControlfor further details on the exceptions it throws.

上面的代码有很多缺点(例如硬编码的“只读”测试),但这只是一个示例,用于说明您可以做什么。DirectoryInfo 有一些其他帮助方法,可用于列出文件夹中的文件。如果您没有访问权限,这些方法将抛出一个UnauthorizedAccessException异常,您可以使用它来测试访问失败的原因。查看有关GetAccessControl的信息,了解有关它引发的异常的更多详细信息。

回答by JaredPar

The #1 most reliable way to determine if you used to havepermission to read from the share is to

确定您是否曾经拥有读取共享权限的 #1 最可靠方法是

  1. Try and read from the share
  2. Handle errors that could occur while reading and consider that a failed attempt
  1. 尝试阅读分享
  2. 处理读取时可能发生的错误并认为尝试失败

Unfortunately though based on your description you are trying to determine if you will haveread permission to the share. There is no way to reliably determine this.

不幸的是,根据您的描述,您正在尝试确定您是否拥有共享的读取权限。没有办法可靠地确定这一点。

No matter how many ACLs, directories, etc ... you look at the moment you're done looking at them you could lose access to the share via any number of mechanisms. The most obvious one is the network share going down. All you can determine is that you used to havepermission to the share.

无论有多少 ACL、目录等……当您查看完它们时,您可能无法通过任意数量的机制访问共享。最明显的一个是网络份额下降。您只能确定您曾经拥有共享权限。