C# .NET - 如何确定目录是否可写(带或不带 UAC)?
我正在开发一款软件,需要将文件复制到文件系统上的给定目录。它需要在支持 UAC 的操作系统(Vista、7)和 XP 上运行。为了解决写入需要 UAC 提升的目录的问题,应用程序实际上启动了另一个进程,其中包含一个声明需要 UAC 的清单。这会生成提示,然后在用户确认时进行复制。
据我所知,一个目录可以具有三种不同的逻辑权限状态:无需UAC提升即可写入、在UAC提升后可写入以及不可写入。
我的问题是:对于给定的目录,如何可靠地确定当前用户是否可以将文件复制(并可能覆盖)到该目录,如果可以,如何确定是否需要 UAC 提升?
在 XP 上,这可能就像检查是否授予“允许写入”权限一样简单,但在 Vista / 7 上,有些目录未授予此权限,但使用 UAC 仍然可以执行此操作。
I'm working on a piece of software that needs to copy a file to a given directory on the filesystem. It needs to work on both UAC-aware OSs (Vista, 7) as well as XP. To get around the issue of writing to a directory where UAC elevation is required, the app actually kicks off another process with a manifest that states that UAC is required. This generates the prompt and then does the copy when the user confirms.
From what I can see, a directory can have three different logical permission states - writeable without UAC elevation, writeable with UAC elevation and not writeable.
My question is this: For a given directory, how do I reliably determine whether the current user can copy (and potentially overwrite) a file to that directory, and if I can, how do I determine if UAC elevation is required?
On XP, this could just be as simple as checking whether the 'Allow Write' permission is granted, but on Vista / 7, there are directories where this permission isn't granted, but this action is still possible with UAC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们有一个用于文件 WriteAccess 的方法,您可以将其改编为目录(Directory.GetAccessControl 等)
希望这会有所帮助。
We have a method for WriteAccess on files, you can probably adapt it for Directories (Directory.GetAccessControl and so on)
Hope this helps.
您只需尝试该操作即可处理可写而无需提升的情况。当失败时,您必须通过 UAC 提升来区分不可写和可写,这可能很困难。
我不认为我希望程序试图为我解决这个问题(因为它们不可避免地会经常出错)。
我认为使用以下假设来设计它是安全的:
因此,总的来说,我建议尝试操作 AsInvoker,如果访问被拒绝,则会弹出一条提示,解释 Windows 拒绝该操作,可能的原因是:文件正在使用、需要提升、需要管理员凭据,并为用户提供三个按钮:
You handle the writable without elevation case just by trying the operation. It's when that fails, and you have to distinguish between not-writable vs writable via UAC elevation that is potentially difficult.
I don't think that I would like programs trying to figure that out for me (since they'll inevitably get it wrong quite often).
I think it's safe to design it with these assumptions:
So altogether, I would suggest trying the operation AsInvoker, in case of access denied bring up a prompt that explains that Windows denied the operation, possible causes are: file-in-use, elevation required, administrator credentials required, and give the user three buttons: