Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 672
MrDatabase Avatar asked Sep 26 '08 00:09

MrDatabase


2 Answers

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!

like image 180
Rob Walker Avatar answered Nov 14 '22 20:11

Rob Walker


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;
like image 38
Treb Avatar answered Nov 14 '22 22:11

Treb