在.NET中,检查当前用户是否可以写入目录

发布于 2024-12-26 17:14:14 字数 291 浏览 0 评论 0原文

在.NET中,是否有一种简单的方法来检查当前用户是否有权在目录中创建文件?与 C++ _access 函数等效的东西将是理想的。

我不想使用反复试验(创建一个虚拟文件,然后将其删除):除了看起来很黑客之外,其他软件正在监视有问题的目录中是否有已删除的文件。

我不想使用 System.DirectoryServices:查看 ACL、解析组成员身份以及不同组成员身份的权限如何交互似乎容易出错且太难。某处一定有一个是或否函数,不是吗?

提前致谢!

[编辑]作为奖励,如果它也适用于网络共享,那就太酷了。

In .NET, is there a simple way to check whether the current user has access to create a file in a directory? Something equivalent to the C++ _access function, would be ideal.

I don't want to use trial and error (create a dummy file and then delete it): apart from seeming hackish, other software is monitoring the directory in question for dropped files.

I don't want to use System.DirectoryServices: looking at ACL's, resolving group memberships and how permissions from different group memberships interact seems error prone and too hard. There must be a yeah-or-nay function somewhere, no?

Thanks in advance!

[edit] as a bonus, if it would work for a network share as well, that'd be cool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扬花落满肩 2025-01-02 17:14:14

提前检查许可是一个冒险的项目。更不用说复杂了,遍历整个ACL列表并计算有效权限集。

此外……没有任何保证。仅仅因为您提前主动检查了权限并不意味着在您尝试创建文件时权限不会更改。

“正确”的方法是提出安全需求,或者用 FileIOPermissionAttribute 或者强制地,通过创建 FileIOPermission 并调用其Demand() 方法。如果您拥有所需的权限,则对 Demand() 的调用会成功;否则它会抛出一个 SecurityException,您需要捕获该异常并采取行动。

根据我的经验,命令式检查更容易。

还应该注意的是,在 Windows 7 中,虽然您在概念上可能具有对该目录的写访问权限,但除非您以提升的权限运行,否则它仍然可能无法工作。

Checking for permission in advance is a dicey project. Not to mention complicated, running through the entire ACL list and computing the effective permission set.

Further...there are no guarantees. Just because you proactively checked for permission ahead of time doesn't mean that the permissions won't have changed at the moment you try to create the file.

The "right" way is to make a security demand, either declaratively with FileIOPermissionAttribute or imperatively, by creating an appropriate instance of FileIOPermission and invoking its Demand() method. If you have the desired permissions, the call to Demand() succeeds; otherwise it throws a SecurityException, which you'll need to catch and act on.

In my experience, the imperative check is easier.

It should also be noted that in Windows 7, while you might conceptually have write access to the directory, it still might not work unless you're running with elevated permissions.

北风几吹夏 2025-01-02 17:14:14
FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, filename);
if (SecurityManager.IsGranted(writePermission)){
    //write here
} else {
    //some error message
}
FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, filename);
if (SecurityManager.IsGranted(writePermission)){
    //write here
} else {
    //some error message
}
沉鱼一梦 2025-01-02 17:14:14

以下将返回 DirecotrySecurity 实例 http://msdn. microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx

DirectoryInfo(pathstr).GetAccessControl()

The following will return a DirecotrySecurity instance http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx

DirectoryInfo(pathstr).GetAccessControl()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文