我的C#应用​​程序如何测试用户是否具有“读取”权限?访问网络共享? [英] How can my C# app test whether the user has "Read" access to a network share?

查看:108
本文介绍了我的C#应用​​程序如何测试用户是否具有“读取”权限?访问网络共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)?

推荐答案

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

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).

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

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

        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...
            }
        }

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

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 UnauthorizedAccessException exception which you can use to test why the access failed. Check out the info on GetAccessControl for further details on the exceptions it throws.

这篇关于我的C#应用​​程序如何测试用户是否具有“读取”权限?访问网络共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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