如果用户有写权限的文件夹C#测试 [英] C# Test if user has write access to a folder

查看:104
本文介绍了如果用户有写权限的文件夹C#测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试,如果用户可以真正的去尝试这样做之前写一个文件夹。

I need to test if a user can write to a folder before actually attempting to do so.

我实现下面的方法(在C#2.0),试图检索使用<这个文件夹的安全权限href=\"http://msdn.microsoft.com/en-us/library/c1f66bc2%28VS.80%29.aspx\">Directory.GetAccessControl()方法。

I've implemented the following method (in C# 2.0) that attempts to retrieve the security permissions for the folder using Directory.GetAccessControl() method.

private bool hasWriteAccessToFolder(string folderPath)
{
    try
    {
        // Attempt to get a list of security permissions from the folder. 
        // This will raise an exception if the path is read only or do not have access to view the permissions. 
        System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

当我在谷歌上搜索如何测试写访问没有像这样走了过来,它似乎很复杂到Windows实际上测试的权限。我很担心,我囫囵吞枣的东西,这个方法并不十分可靠,但它似乎工作。

When I was googling how to test for write access nothing like this came up and it appeared very complicated to actually test permissions in Windows. I am concerned that I am over-simplifying things and that this method is not robust, although it does seem to work.

请问我的方法来测试当前用户是否拥有正确的写访问工作?

Will my method to test if the current user has write access work correctly?

推荐答案

这是检查在C#中文件夹的访问是非常有效的方法。它可能会掉下来的唯一的地方是,如果你需要在一个紧密的循环,一个异常的开销的可能的是一个问题调用此。

That's a perfectly valid way to check for folder access in C#. The only place it might fall down is if you need to call this in a tight loop where the overhead of an exception may be an issue.

还有其他<一href=\"http://stackoverflow.com/questions/1281620/checking-for-directory-and-file-write-permissions-in-net\">similar <一href=\"http://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file\">questions问previously。

There have been other similar questions asked previously.

这篇关于如果用户有写权限的文件夹C#测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆