Relative path problem for a deployed win32 application
I have written a c++ program and deployed it in say c:\my_app
, and my executable's path is c:\my_app\my_app.exe
. Say, my_app needs many files such as the_file.txt
, which is located in c:\my_app\the_file.txt
.
In my executable, I open the txt file as, xx.open("the_fi开发者_Python百科le.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt
. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT
? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt"
. I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app.... Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path
精彩评论