Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have created a folder in which I have put my executable and its config file. I am using Windows XP.
When I start the executable after the PC has already started up, by double-clicking on the exe, it finds the config file.

I have put an entry in the registry to start my executable at startup. When it starts up from PC startup, it does not find the config file. I have found that the current directory when the code is running in this case is C:\Documents and settings\DEFAULT however when I start the exe after the PC has started the current directory is where the exe is running from, which is what i would expect.

Can anyone please tell me why the current directory is different when the exe is started up at start-up? Is there a way to set the current directory to the directory that the exe is in at startup?

Thank you for any help.
Posted
Updated 23-Jul-12 15:45pm
v2

1 solution

The current directory of your exe can be anything when you start it. Try this: create a folder called "X" and put your exe inside this folder. go outside this x folder in your console and start your exe by typing in "X\myprogram.exe". in this case the current directory of your prog will be the directory that was the current dir of your console. To get your config file from the directory your exe is located you can do one of these:
- In a console program argv[0] will be "X\myprogram.exe" in my example so you can strip off the filename and append the config filename to the "X\" so it will result in "X\myprogram.cfg".
- On Windows call GetModuleFilename(NULL) that gives back the full path of your exe so you can change the filename and you get a full path to your config file.
 
Share this answer
 
v2
Comments
Jackie Lloyd 23-Jul-12 11:33am    
Thankyou very much, GetModuleFilname is exactly what I need. :)
Sergey Alexandrovich Kryukov 23-Jul-12 16:46pm    
Exactly; my 5.
--SA
Albert Holguin 23-Jul-12 21:45pm    
+5
Malli_S 24-Jul-12 2:01am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900