views:

2062

answers:

4

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

+2  A: 

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!

Rob Walker
+2  A: 

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.

ScottKoon
+1  A: 

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

Dr8k
+1  A: 

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;
Treb
Remember that it is possible for a Windows user to HAVE write permissions to a file or folder but NOT have delete permission to the same (with write but NOT modify)
hromanko