{"id":9433,"date":"2011-10-10T07:00:00","date_gmt":"2011-10-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/10\/10\/how-do-i-access-a-file-without-updating-its-last-access-time\/"},"modified":"2011-10-10T07:00:00","modified_gmt":"2011-10-10T07:00:00","slug":"how-do-i-access-a-file-without-updating-its-last-access-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20111010-00\/?p=9433","title":{"rendered":"How do I access a file without updating its last-access time?"},"content":{"rendered":"<p>\nThe first problem with discussing file last-access time is\nagreeing what you mean by a file&#8217;s last-access time.\n<\/p>\n<p>\nThe file system folks have one definition of the file last-access time,\nnamely the time the file was most recently opened and either read from\nor written to.\nThis is the value retrieved by functions like\n<code>Get&shy;File&shy;Attributes&shy;Ex<\/code>,\n<code>Get&shy;File&shy;Time<\/code>,\nand <code>Find&shy;First&shy;File<\/code>.\n<\/p>\n<p>\nThe problem with this definition is that it doesn&#8217;t match the\nintuitive definition of last-access time, which is\n&#8220;the last time <i>I<\/i> accessed the file,&#8221;\nemphasis on the <i>I<\/i>.\nIn fact, the intuitive definition of access is more specific:\nIt&#8217;s\n&#8220;the last time I opened, modified, printed, or otherwise performed\nsome sort of purposeful action on the file.&#8221;\n<\/p>\n<p>\nThis discrepancy between the file system definition and the intuitive\ndefinition means that a lot of operations trigger a file system access\nbut shouldn&#8217;t count as an access from the user interface point of view.\nHere are some examples:\n<\/p>\n<ul>\n<li>Accessing the file to generate a preview or thumbnail image.\n<li>Accessing the file to\n    <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/02\/17\/9426787.aspx#9430964\">\n    show its property sheet<\/a>.<\/p>\n<li>Accessing the file to retrieve its icon.\n<li>Accessing the file to retrieve properties to show in Explorer\n    or some other viewer.<\/p>\n<li>Accessing the file in order to back it up.\n<li>Accessing the file in order to display its context menu,\n    <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/10\/03\/10218997.aspx\">\n    something you aren&#8217;t supposed to be doing in the first place<\/a>\n    but people\n    <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/10\/05\/9903476.aspx#9904013\">\n    do it anyway<\/a>.\n<\/ul>\n<p>\nWhenever some shell extension violates this rule,\nthe shell team gets a bug report from some customer saying,\n&#8220;The last-access time shown in Explorer is wrong.\nA document which hasn&#8217;t been accessed in months shows a last-access\ntime of today.\nAfter closer investigation, we found that the last-access time updates\nwhenever we\n<i>insert seemingly-innocuous operation here<\/i>.&#8221;\n<\/p>\n<p>\nIf you&#8217;re writing a program which needs\nto access the file contents but you not want to update\nthe last-access time, you can use the <code>Set&shy;File&shy;Time<\/code>\nfunction\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/ms724933.aspx\">\nwith the special value <code>0xFFFFFFFF<\/code> in both members\nof the <code>FILETIME<\/code> structure passed\nas the last-access time<\/a>.\nThis magic value means &#8220;do not change the last-access time\neven though I&#8217;m accessing the file.&#8221;\n<\/p>\n<pre>\nBOOL DoNotUpdateLastAccessTime(HANDLE hFile)\n{\n static const FILETIME ftLeaveUnchanged = { 0xFFFFFFFF, 0xFFFFFFFF };\n return SetFileTime(hFile, NULL, &amp;ftLeaveUnchanged, NULL);\n}\n<\/pre>\n<p>\nAs the documentation notes, you have to call this function\nimmediately after opening the file.\n<\/p>\n<p>\nGoing back to that linked comment:\nThe reason why viewing the Summary tab causes the last-access time\nto be updated is that the Summary tab retrieves its information\nby calling <code>Stg&shy;Open&shy;Storage<\/code>, and there&#8217;s no way to tell\nthat function,\n&#8220;Hey, when you open the file in order to see if it has any\ndocument properties, do that\n<code>Do&shy;Not&shy;Update&shy;Last&shy;Access&shy;Time<\/code>\nthing so you don&#8217;t update the last access time.&#8221;\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nStarting in Windows Vista,\n<a HREF=\"http:\/\/blogs.technet.com\/b\/filecab\/archive\/2006\/11\/07\/disabling-last-access-time-in-windows-vista-to-improve-ntfs-performance.aspx\">\nmaintaining the last-access time is disabled by default<\/a>.\nIn practice, this means that the number of bugs related to\naltering the last-access time accidentally will multiply unchecked,\nbecause the mechanism for detecting the error is disabled by default.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first problem with discussing file last-access time is agreeing what you mean by a file&#8217;s last-access time. The file system folks have one definition of the file last-access time, namely the time the file was most recently opened and either read from or written to. This is the value retrieved by functions like Get&shy;File&shy;Attributes&shy;Ex, [&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-9433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The first problem with discussing file last-access time is agreeing what you mean by a file&#8217;s last-access time. The file system folks have one definition of the file last-access time, namely the time the file was most recently opened and either read from or written to. This is the value retrieved by functions like Get&shy;File&shy;Attributes&shy;Ex, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9433","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=9433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9433\/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=9433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=9433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=9433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}