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

痴心易碎 提交于 2019-12-09 06:30:25

问题


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


回答1:


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

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

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.




回答2:


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

  1. Try and read from the share
  2. Handle errors that could occur while reading and consider that a failed attempt

Unfortunately though based on your description you are trying to determine if you will have read 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 have permission to the share.



来源:https://stackoverflow.com/questions/753120/how-can-my-c-sharp-app-test-whether-the-user-has-read-access-to-a-network-shar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!