{"id":12293,"date":"2010-11-12T07:00:00","date_gmt":"2010-11-12T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/11\/12\/why-does-the-common-file-dialog-change-the-current-directory\/"},"modified":"2010-11-12T07:00:00","modified_gmt":"2010-11-12T07:00:00","slug":"why-does-the-common-file-dialog-change-the-current-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101112-00\/?p=12293","title":{"rendered":"Why does the common file dialog change the current directory?"},"content":{"rendered":"<p>When you change folders in a common file dialog, the common file dialog calls <code>Set&shy;Current&shy;Directory<\/code> to match the directory you are viewing. (Don&#8217;t make me bring back the Nitpicker&#8217;s Corner.)\n Okay, the first reaction to this is, &#8220;What? I didn&#8217;t know it did that!&#8221; This is the other shoe dropping in the <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/11\/09\/10087919.aspx\"> story of the curse of the current directory<\/a>.\n Now the question is, &#8220;Why does it do this?&#8221;\n Actually, you know the answer to this already. <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/11\/10\/10088566.aspx\"> Many programs require that the current directory match the directory containing the document being opened<\/a>.\n Now, it turns out, there&#8217;s a way for you to say, &#8220;No, I&#8217;m not one of those lame-o programs. I can handle current directory being different from the document directory. Don&#8217;t change the current directory when using a common file dialog.&#8221; You do this by passing the <code>OFN_NO&shy;CHANGE&shy;DIR<\/code> flag. (If your program uses the <code>IFile&shy;Dialog<\/code> interface, then <code>NO&shy;CHANGE&shy;DIR<\/code> is always enabled. Hooray for progress.)\n But now that you know about this second curse, you can actually use it as a counter-curse against the first one.\n If you determine that a program is holding a directory open, and you suspect that it is the victim of the curse of the current directory, you can go to that program and open a common file dialog. (For example, <i>Save As<\/i>.) From that dialog, navigate to some other directory you don&#8217;t plan on removing, say, the root of the drive, or your desktop. Then cancel the dialog.\n Since the common file dialog changes the current directory, you have effectively injected a <code>Set&shy;Current&shy;Directory<\/code> call into the target process, thereby changing it from the directory you want to remove. Note, however, that this trick works only if the application in question omits the <code>OFN_NO&shy;CHANGE&shy;DIR<\/code> flag when it calls <code>Get&shy;Save&shy;File&shy;Name<\/code>.<\/p>\n<p> In Explorer, you can easily call up a common file dialog by typing Win+R then clicking Browse, and in versions of Windows up through Windows&nbsp;XP, Explorer didn&#8217;t pass the <code>OFN_NO&shy;CHANGE&shy;DIR<\/code> flag. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you change folders in a common file dialog, the common file dialog calls Set&shy;Current&shy;Directory to match the directory you are viewing. (Don&#8217;t make me bring back the Nitpicker&#8217;s Corner.) Okay, the first reaction to this is, &#8220;What? I didn&#8217;t know it did that!&#8221; This is the other shoe dropping in the story of the [&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":[26],"class_list":["post-12293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>When you change folders in a common file dialog, the common file dialog calls Set&shy;Current&shy;Directory to match the directory you are viewing. (Don&#8217;t make me bring back the Nitpicker&#8217;s Corner.) Okay, the first reaction to this is, &#8220;What? I didn&#8217;t know it did that!&#8221; This is the other shoe dropping in the story of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12293","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=12293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12293\/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=12293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}