windows 在 Win2K/XP 中检查目录写权限的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/198071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code for checking write-permissions for directories in Win2K/XP
提问by Evan
Greetings!
你好!
I am trying to check directory write-permissions from within a Windows MFC/ATL program using C++. My first guess is to use the C-standard _access function, e.g.:
我正在尝试使用 C++ 从 Windows MFC/ATL 程序中检查目录写入权限。我的第一个猜测是使用 C 标准的 _access 函数,例如:
if (_access("C:\mydir", 2) == -1)
// Directory is not writable.
But apparently on Windows 2000 and XP, _access can't determine directory permissions. (i.e. the Security tab in the Properties dialog when you right-click on a directory in Explorer) So, is there an elegant way to determine a directory's write-permissions in Windows 2000/XP using any of the Windows C++ libraries? If so, how?
但显然在 Windows 2000 和 XP 上,_access 无法确定目录权限。(即,当您在资源管理器中右键单击目录时,“属性”对话框中的“安全”选项卡) 那么,是否有一种优雅的方法可以使用任何 Windows C++ 库来确定 Windows 2000/XP 中目录的写权限?如果是这样,如何?
Thanks
谢谢
Evan
埃文
采纳答案by Frederik Slijkerman
You can call CreateFile with GENERIC_WRITE access to check this. http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
您可以调用具有 GENERIC_WRITE 访问权限的 CreateFile 来检查这一点。 http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
It's not a C++ library but it still counts as elegant because it directly does what you want...
它不是一个 C++ 库,但它仍然算优雅,因为它直接做你想要的......
回答by Nick
There are many Windows security functions, although I wouldn't call their usage particularly elegant. I would recommend GetNamedSecurityInfo as a general solution, but the CreateFile solution has a certain simplicity and directness which I can also appreciate. :)
有许多Windows 安全功能,尽管我不会称它们的使用特别优雅。我会推荐 GetNamedSecurityInfo 作为通用解决方案,但 CreateFile 解决方案具有一定的简单性和直接性,我也很欣赏。:)
回答by Nick
Use sec api. You can ask on Adv. Win32 api newsgroup : news://194.177.96.26/comp.os.ms-windows.programmer.win32 where it has often been discussed (C/C++ code)
使用秒 api。你可以问Adv。Win32 api 新闻组:news://194.177.96.26/comp.os.ms-windows.programmer.win32 经常讨论的地方(C/C++ 代码)