简体   繁体   中英

MFC C++ Reference INI file in application folder after deployment

Hoping for some help here, as I am tearing my hair out.I'm using Visual Studio 2010.

I have an MFC C++ application to deploy. I have a config.ini file that I'd like referenced when the exe starts. At the moment the user cannot double click on a *.myfile and it open, because the application does not "start in" the application folder (where I am installing the ini file to), and so cannot find the ini. I've tried the following

  • I tried finding info on setting the "start in" folder for the &Open action, but cannot find anything.

  • I can't find any info on setting a registry value of the ini file at installation, since this would be a relative reference depending on the user's choice, and so this doesn't apply.

  • It is unmanaged so the C++/CLI app.config solution doesn't apply.

  • Using the Application Data folder, but this gives me the wrong path - I'm using Windows 7, this is probably why, but I want my deployment to work on Windows XP ++.

  • Reading the app path at start up (from here ) (put this in CMyApp::InitInstance().

Code:

CString strPath;
TCHAR* pstrExePath = strPath.GetBuffer (MAX_PATH);
::GetModuleFileName (0, pstrExePath, MAX_PATH);
strPath.ReleaseBuffer();
int pos = strPath.ReverseFind('\\');
strPath = strPath.Left(pos);
strPath += "\\config.ini";

This is the closest, but in debug mode there is a weird "\.\" in the path invalidating it. I could use a #ifdebug but this is messy surely?

Really appreciate any help - thanks!


EDIT:

Using this:

TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath)))
{
    MessageBox(NULL,szPath, "MyApp", MB_OK|MB_ICONWARNING);
}

I get: "C:\ProgramData" as the szPath. Maybe this is right for debug mode? Or the development machine?

Thanks for all the input and prompts. It helped me get to this solution. Hopefully it helps some others to have the info in one place. This solution is a very simple one and probably not for full commercial deployment!

  • In VS2010 in the FileSystem (Setup) put the config file in the User's Application Data Folder \ Productname
  • Set InstallAllUsers to false, so that you don't need conditionals on where your config file is located, based on the user's installation choice
  • In the InitInstance() function add something like the following:

[listbreaker]

TCHAR szPath[MAX_PATH] = {0};
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,NULL,SHGFP_TYPE_CURRENT,szPath))) 
{
    PathAppend(szPath, "\\MyApp\\");
            // Can check to create the directory here
    //if (!PathFileExists(szPath))
    //  CreateDirectory(szPath, NULL);
    PathAppend(szPath, TEXT("MyApp.ini"));
            // can check to create default file here if removed by user
    //if (!PathFileExists(szPath))
        //g_ConfigData.write_default()  // be nice if it could write itself
    //MessageBox(NULL,szPath, "MyApp", MB_OK|MB_ICONWARNING);
}
if (!g_ConfigData.ReadData( szPath ) )
    MessageBox(NULL,"Configuration file cannot be read", "MyApp", MB_OK|MB_ICONWARNING);

Some useful links that really helped me on this are:

I'd appreciate any further help on this, as I'm sure there are more refined and flexible solutions (eg handling "All Users" choice on installation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM