测试文件/目录是否为只读 [英] Testing if a file/directory is read only

查看:26
本文介绍了测试文件/目录是否为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有点像 C 新手.如何测试一个文件在windows上是否只读.

okay, so I'm a bit of a C newbie. How does one test whether a file is read only on windows.

我尝试使用 GetFileAttributes 函数进行一些操作,但无济于事.

I tried to get something working with the GetFileAttributes function, but to no avail.

给定文件的路径,测试它是否为只读的正确方法是什么?

Given a file's path, what is the proper way to test if it is read only?

所以我仍然坚持这个.我想检查用户是否有权添加和编辑 C:\Program Files... 中的文件,但我似乎无法获得任何建议.

So I'm still stuck on this one. I want to check if the user has permission to add and edit files in C:\Program Files... and I can't seem to get any of the advice to work.

当我使用 !(GetFileAtrributes(path) & FILE_ATTRIBUTES_READONLY) 时,它表示该目录不是只读的.然后我尝试编写一个文件并测试它是否有效:

when I use !(GetFileAtrributes(path) & FILE_ATTRIBUTES_READONLY), it indicates that the directory is not read only. I then tried to write a file and test if it worked:

strcat(path,"\\testFile000");
FILE *test = 0;
test = fopen(path,"w");
int i = fwrite("ab",1,sizeof("ab"),test);

但是当我做这个测试是非空的,i==3 并且没有文件出现在操作系统中.

but when I do this test is non-null, i==3 and no file appears in the OS.

有什么想法吗?

推荐答案

您忘记了 ACL.文件可以将它的只读位清除,但仍然具有限制性 ACL.您可以调用 CreateFile 并检查返回代码.或者调用AuthzAccessCheck".

You are forgetting about the ACL. A file can have it's read-only bit clear but still have a restrictive ACL. You can call CreateFile and check the return code. Or call `AuthzAccessCheck'.

这篇关于测试文件/目录是否为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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