{"id":95946,"date":"2017-04-12T07:00:00","date_gmt":"2017-04-12T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95946"},"modified":"2019-03-13T01:09:28","modified_gmt":"2019-03-13T08:09:28","slug":"20170412-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170412-00\/?p=95946","title":{"rendered":"How do I get the current directory for a non-current drive?"},"content":{"rendered":"<p>As we learned some time ago, each drive has its own current directory <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20101011-00\/?p=12563\">for backward-compatibility with DOS 1.0<\/a> (which predates the concept of directories). This is simulated internally by <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20100506-00\/?p=14133\">magic environment variables<\/a>. But those magic environment variables are internal and are therefore not contractual. <\/p>\n<p>What is the supported way of finding out the current directory for a non-current drive? <\/p>\n<p>One approach I&#8217;ve seen is to perform this sequence of operations: <\/p>\n<pre>\nGetCurrentDirectory(ARRAYSIZE(originalDirectory), originalDirectory);\nSetCurrentDirectory(TEXT(\"X:\"));\nGetCurrentDirectory(ARRAYSIZE(currentDirectoryOnX), currentDirectoryOnX);\nSetCurrentDirectory(originalDirectory);\n<\/pre>\n<p>One problem with this approach is that it may not be able to return to the original directory if the original directory was deleted or is otherwise not settable. <\/p>\n<p>Another problem with this approach is that it applies a global solution to a local problem: The current directory is a process-wide resource, so if there is another thread that is performing an operation that depends on the current directory, you just messed up that thread. <\/p>\n<p>The supported way to get the current directory for a non-current drive is to call <code>Get&shy;Full&shy;Path&shy;Name<\/code> and pass just the drive letter and a colon. This will resolve the drive letter to the current directory for that drive. <\/p>\n<pre>\nTCHAR buf[BUFFERSIZE];\nTCHAR *file;\nGetFullPathName(TEXT(\"X:\"), BUFFERSIZE, buf, &amp;file);\n<\/pre>\n<p>Assuming the call succeeds, the result in the buffer is the current directory for drive <code>X:<\/code>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>GetFullPathName will tell you.<\/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-95946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>GetFullPathName will tell you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95946","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=95946"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95946\/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=95946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}