Skip to main content
gamedev.net
🔒 Locked

preserve current directory

Started by JohnnyCode Feb 25, 2009 at 10:22 AM 5 replies 600+ views
Original Post
JohnnyCode
JohnnyCode
hello, I would like to have my application not changing its current directory. Is there a way to set current directory and make sure it does not change?
Extrarius
Extrarius
What causes the current directory to change in your application, and why do you need it to remain the same?

If you're referencing data files using relative paths on windows, I strongly suggest you get the directory of the executable and manually adjust all relative paths using the application's directory so that you don't have to rely on the current directory since you can't count on it being anything in particular.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
JohnnyCode
JohnnyCode
I have many file load dialogs, and every such a dialog changes current working directory.So I have to allways manually reset it to my desired dir. I would like to get rid of it... and it also gives me this strange feeling of not knowing where else it can happen, so I am always nervous through the life of my app.
Anon Mike
Anon Mike
There's no way to prevent it from being changed by some component in your process. You should either not use those components, reset the directory before doing your stuff, or not use relative paths in the first place (e.g. build up the correct path at startup).
-Mike
SiCrane
SiCrane
Depending on how you are creating the file load dialogs and the platform you're programming on, there may be mechanisms in place to prevent the current directory from changing. For example, OFN_NOCHANGEDIR with GetOpenFileName() would stop it from changing the working directory on the non-NT based Windows operating systems.
Deyja
Deyja
Contrary to MS' documentation, it's been my experience that OFN_NOCHANGEDIR also works on NT.
mutex
mutex
Quote:
Original post by Deyja
Contrary to MS' documentation, it's been my experience that OFN_NOCHANGEDIR also works on NT.
If it isn't documented I would not rely on it. Relying on undocumented things: a) may break your app when the undocumented thing changes, and b) causes pain for the people who wrote GetOpenFileName when they want to change it.

Personally it's probably better to use absolute path names throughout your application.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.