{"id":39023,"date":"2004-06-04T07:00:00","date_gmt":"2004-06-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/06\/04\/an-easy-way-to-determine-whether-you-have-a-particular-file-permission\/"},"modified":"2004-06-04T07:00:00","modified_gmt":"2004-06-04T07:00:00","slug":"an-easy-way-to-determine-whether-you-have-a-particular-file-permission","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040604-00\/?p=39023","title":{"rendered":"An easy way to determine whether you have a particular file permission"},"content":{"rendered":"<p>Sometimes you might want to determine whether you <strong>can<\/strong> do something without actually doing it. For example, you might want to know whether you have a particular permission in a directory, say permission to delete files from it. <\/p>\n<p>One way is to retrieve the ACL and then check whether the current user has the desired permission. The <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/security\/security\/accesscheck.asp\">AccessCheck function<\/a> does most of the heavy lifting there. <\/p>\n<p>Or you can realize, &#8220;Hey, wait a second, there is an entire security infrastructure whose job it is to decide who can access which files. Why not use it?&#8221; <\/p>\n<p>For example, here&#8217;s how you can check whether the user has permission to delete files from a directory: <\/p>\n<pre>BOOL CanDeleteFilesFromDirectory(LPCTSTR pszPath)\n{\n  HANDLE h = CreateFile(pszPath, FILE_DELETE_CHILD, FILE_SHARE_READ |\n                        FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,\n                        OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);\n  if (h != INVALID_HANDLE_VALUE) {\n    CloseHandle(h);\n  }\n  return h != INVALID_HANDLE_VALUE;\n}\n<\/pre>\n<p>What we did was open the directory (which requires backup semantics) and ask for FILE_DELETE_CHILD access. If it succeeded, then we have permission to delete files from it. [Corrected 7:52am]<\/p>\n<p>Note, of course, that this information is purely advisory. You shouldn&#8217;t be making security decisions based on this, because the permissions might change between the time you check and the time you try. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you might want to determine whether you can do something without actually doing it. For example, you might want to know whether you have a particular permission in a directory, say permission to delete files from it. One way is to retrieve the ACL and then check whether the current user has the desired [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-39023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Sometimes you might want to determine whether you can do something without actually doing it. For example, you might want to know whether you have a particular permission in a directory, say permission to delete files from it. One way is to retrieve the ACL and then check whether the current user has the desired [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=39023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=39023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=39023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=39023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}