Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to check the fodler permissions for currently logged in User in a windows application.

Eg: i have a folder in "e" drive with folder name "test" (E:\\test). the current logged in user is "nidheesh-pc\nidheesh". but the administrator removed (deny) all the permissions for the folder "E:\\test" for the user "nidheesh-pc\nidheesh".

Now while opening the application i need to check whether "nidheesh-pc\nidheesh" is having permissions for the given folder.

if there is no permission i need to thow a error message to user.

Hope the Question is clear

Thanks,
Nidheesh
Posted

I use simple practical approach in this case. It's called offensive programming as opposed to defensive programming.

Here is what you do. Suppose you need the write access to the folder to write a file. Do the following:
C#
bool CanCreateFile(string fileName) {
    try {
        System.IO.StreamWriter writer =
            new System.IO.StreamWriter(fileName, false);
        if (writer != null) writer.Close();
        return true;
    catch { return false; }
}


Using this idea, you can do a more fine-grain check to see if the file is already exist, which also can show the lack of read access. When you do the check, you show something like "Access denied" to the user anyway, right? In real life, think if you need this check at all. I never do it, I just try to read or write and create file and deal with exception. In this case, I show sufficient and accurate exception information to the user. This is safer (no exceptions hidden by catching them prematurely and blocking their propagation), simpler and more robust.

—SA
 
Share this answer
 
Hope Check User’s Permissions On A File or Folder[^] article from CP might help you.
 
Share this answer
 
Comments
nidheeshkayal 7-Jun-11 4:37am    
this is with VC++. i need C#.net code.

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900