How can I programmatically determine if I have write privileges using C# in .Net?

女生的网名这么多〃 提交于 2019-11-30 08:45:39

问题


How can I determine if I have write permission on a remote machine in my intranet using C# in .Net?


回答1:


The simple answer would be to try it and see. The Windows security APIs are not for the faint of heart, and may be possible you have write permission without having permission to view the permissions!




回答2:


Been there too, the best and most reliable solution I found was this:

bool hasWriteAccess = true;
string remoteFileName = "\\server\share\file.name"

try
{
    createRemoteFile(remoteFileName);   
}
catch (SystemSecurityException)
{
     hasWriteAccess = false;   
}

if (File.Exists(remoteFileName))
{
    File.Delete(remoteFileName);
}

return hasWriteAccess;



回答3:


Check out this forum post.

http://bytes.com/forum/thread389514.html

It describes using the objects in the System.Security.AccessControl namespace to get a list of the ACL permissions for a file. It's only available in .NET 2.0 and higher. I think it also assumes that you have an SMB network. I'm not sure what it would do if you were using a non-Windows network.

If you aren't on .NET 2.0 or higher, it's the usual pInvoke and Win32 API jazz.




回答4:


ScottKoon is write about checking the windows ACL permissions. You can also check the managed code permissions using CAS (Code Access Security). This is a .Net specific method of restricting permissions. Note, if the user doesn't have write permissions then the code will never have write permissions (even if CAS says it does) - the most restrictive permissions between the two win.

CAS is pretty easy to use - you can even add declarative attributes you the start of your methods. You can read more at MSDN



来源:https://stackoverflow.com/questions/137031/how-can-i-programmatically-determine-if-i-have-write-privileges-using-c-sharp-in

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