Check for "Write Access" on a file/folder in .NET

G

Guest

Hello All,
Our application write logs to a file in a folder. Before our application
starts writing to that file, I want to check if the current user has write
access to that file, for example, "c:\temp\LogFile.txt".
I see several articles for setting file access permissions, getting file
access permissions for a given user or current user - but the current user
could also gain write access to the same file not just by explicit permssion
but also thru implicit permissions. for example, the current user may be in
a NT group which can have write access to that file - so this user also gets
write access.
How to check if the current user can have write access either thru explicit
or implcit permssions set on the folder/file?.

All the following articles below talk about retrieving explicit permssions
or setting permissions but nothing talks about checking if the user has
access to that file thru implicit permssions.
In short, I want to check if the current user has write access to the file
"c:\temp\LogFile.txt".

Can someone help?

File.GetAccessControl Method
http://msdn2.microsoft.com/en-us/library/system.io.file.getaccesscontrol.aspx

FileSecurity Class
http://msdn2.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx

FileSecurity Members
http://msdn2.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity_members.aspx
 
S

Stephany Young

Open the file for 'write' access, with the statement wrapped in a try catch.

If the statement succeeds then the user has 'write' access.

If the statement throws an exception then inspect the exception to determine
the reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top