After a call of GetOpenFileName
the current directory of the process changes to the directory of the file opened file by the GetOpenFileName
.
How can I keep the default current directory instead ?
The cd (change directory) command allows you to move around within the file system hierarchy: $ cd /usr/lib $ pwd /usr/lib. When you type the cd command by itself, you return to your home directory.
The current working directory is the directory in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory. By default, when you log into your Linux system, your current working directory is set to your home directory.
Updated: 12/30/2019 by Computer Hope. Alternatively referred to as the working directory or current working directory (CWD), the current directory is the directory or folder where you are currently working.
How can I keep the default current directory instead ?
If you read the OPENFILENAME
documentation, there is an OFN_NOCHANGEDIR
flag for that exact purpose:
Restores the current directory to its original value if the user changed the directory while searching for files.
Despite what the documentation claims, this flag is supported in GetOpenFileName()
.
Also see Raymond Chen's blog article on this subject:
Why does the common file dialog change the current directory?
The current directory exists because it is very convenient for command line tools. It generally isn't very much use for GUI applications, which is probably why Microsoft's developers didn't worry about allowing GetOpenFileName() to change it. There is of course the occasional edge case, and you might be dealing with one of them, although it is hard to tell from your question as written. (Are you absolutely sure that you want the current directory and not, for example, the directory containing your executable?)
At any rate, if you do want the current directory, the safest approach is to retrieve it as soon as the program starts, and use that saved value to construct fully qualified paths. Don't just restore the original current directory whenever you think it might have been changed, build the fully qualified paths yourself. This is especially important in multi-threaded code, or in code that might need to be multi-threaded in the future (i.e., pretty much everything) but it also eliminates the risk of overlooking one or more code paths where the current directory might change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With