且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查文件夹权限

更新时间:2022-10-20 19:00:25

此链接将为您提供帮助
http://***. com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file [ ^ ]

在这种情况下,我使用了简单实用的方法.与防御性编程相反,它被称为攻击性编程.

这就是你要做的.假设您需要对该文件夹具有写访问权才能写入文件.请执行以下操作:
  bool  CanCreateFile( string  fileName){
    尝试 {
        System.IO.StreamWriter writer =
             System.IO.StreamWriter(fileName, false );
        如果(作家!= )writer.Close();
        返回 捕获 {返回  false ; }
} 



使用此想法,您可以进行更细粒度的检查,以查看文件是否已存在,这也可能表明缺少读取访问权限.当您进行检查时,还是会向用户显示类似拒绝访问"的信息,对吧?在现实生活中,请考虑是否完全需要此检查.我从不这样做,我只是尝试读取或写入并创建文件并处理异常.在这种情况下,我向用户显示了足够且准确的异常信息.这样更安全(过早捕获并阻止其传播不会隐藏任何异常),更简单且更可靠.

-SA


希望在A上检查用户权限文件或文件夹 [ ^ ]文章CP提供的帮助可能会对您有所帮助.


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

this link will help you
http://***.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file[^]


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:
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


Hope Check User’s Permissions On A File or Folder[^] article from CP might help you.