检查目录的正确方法可用于写入

发布于 2024-12-28 06:57:06 字数 621 浏览 1 评论 0原文

我最近添加了 GetTempPath 的使用 到应用程序。在代码审查期间,突出显示 GetTempPath 的描述包含以下信息:

应用程序应在使用文件 I/O 操作之前验证路径是否存在以及对该路径的足够访问权限。

现在所有文件访问都包含在 try/catch 块中,并且哪个系统无法访问其自己的临时目录?

我最初的想法是尝试创建目录(如果目录不存在)(通过 GetFileAttributes 和 CreateDirectory),然后创建一个文件,写入一个字节,然后删除该文件。虽然这可行,但有点无知——肯定有更好的方法来检查您是否具有对文件夹的写访问权限?

我开始查找,发现了文件属性常量、通用访问权限、标准访问权限、文件访问权限常量和 GetSecurityInfo 函数。所有这些似乎都创建了一个比创建文件并查看它是否有效的方法更长的解决方案。

那么使用 WinAPI 函数检查您是否具有文件夹的写入权限的正确方法是什么?

I recently added the use of GetTempPath to the application. During code review it was highlighted that the description of GetTempPath contains the information:

The application should verify the existence of the path and adequate access rights to the path prior to any use for file I/O operations.

Now all file access is wrapped in try/catch blocks and what system will there be that doesn't have access to it's own temp directory?

My initial idea would be to attempt creating the directories if they don't exist (via GetFileAttributes and CreateDirectory) and then to create a file, write a byte and then delete the file. While that will work it smacks of ignorance - surely there is a better way of checking that you have write access to a folder?

I started looking and found File Attribute Constants, Generic Access Rights, Standard Access Rights, File Access Rights Constants and GetSecurityInfo function. All of which seemed to create a solution that is longer than the create a file and see if it sticks method.

So what is the correct way using WinAPI functions of checking you have write access to a folder?

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

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

发布评论

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

评论(2

冰魂雪魄 2025-01-04 06:57:06

MSDN 注释具有误导性。您可能想检查返回的路径是否存在(如果不存在,则创建它),但知道是否可以在那里写入的唯一方法就是在那里写入。

此外,仅仅因为您现在可以在那里写并不意味着您以后可以在那里写。用户或其他程序可能会删除内容、更改安全设置、锁定目录等。尝试提前验证它是否是可以写入的位置是浪费时间。只在需要的时候写,并做好失败的准备。

The MSDN note is misleading. You might want to check to see if the path returned exists (and if not, create it), but the only way to know if you can write there is to write there.

Furthermore, just because you can write there now doesn't mean you can write there later. The user or other programs may delete things, change security settings, lock directories, etc. Trying to validate ahead of time that it's a place you can write is a waste of time. Just write when you need to, and be prepared for failure.

感性 2025-01-04 06:57:06

最好的方法是尝试将文件写入该目录。如果由于缺乏权限而导致写入失败,则错误代码会告诉您这一点。查找ERROR_ACCESS_DENIED

如果您尝试以任何其他方式执行此操作,那么您将只是复制即将运行的系统代码。而且您几乎没有机会为当前版本的 Windows 和未来版本完美地复制它。

无需创建文件并向其中写入单个字节。假设您对临时文件夹具有写入权限,并尝试写入所需的整个文件。如果您遇到失败,那么您也可以终止该过程。如果您无法写入临时文件夹,则继续进行就没有意义。

The best way is to attempt to write files to the directory. If writing fails due to lack of rights, then the error code will tell you that. Look for ERROR_ACCESS_DENIED.

If you try and do it any other way then you will just be replicating system code that was about to run anyway. And there's little chance for you to replicate it perfectly for current versions of Windows and future versions.

There's no need to create a file and write a single byte to it. Just assume that you have write access to the temporary folder and attempt to write the entire file(s) that you need to. If you ever encounter a failure then you may as well terminate the process. There's not much point in proceeding if you can't write to the temporary folder.

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