Saving a file to / loading a file from a specific folder

This is probably a no brainer but I'm not sure of the best way of making it happen.  I have a MIDI application, 4 different docment types.  4 of these are created by the user using a maintenance facility designed for each.  These can also be loaded for additional file maintenance and are selected and loaded as input into another function of this application.  When the application executes and completes 3 additional document types are created. These 3 created documents can subsequently be loaded into the application for analysis by yet a different set of functions.

The way this application currently exists, all documents are crated in, loaded from and saved back in the same folder containing the executable.  That was fine for development but is crappy for a delivered application.

What I am looking for is a way to save the 3 application created documents in a specific folder, say a folder in the "My Documents" directory.  I would also like to save the 4 user created / user maintained documents is a folder of their own, which can be in the "My Documents" directory or in a folder below the "Programs" directory where the application resides.

I need to be able to direct the "Open File Dialog" for all of these documents to automatically present the appropriate folder for the user to view wneh he/she decides to open one of these documents.

It may be as simple as changing the directory via
SetCurrentDirectory( lpPathName )

But I think that is dangerous if the application craps out before the directory can be reset back to its original setting, screwing up the user's environment.

Is my fear naive? Is this the correct approach? Is there a better way?

Thanks in advance.
Who is Participating?
jkrConnect With a Mentor Commented:
>>I need to be able to direct the "Open File Dialog" for all of these documents to
>>automatically present the appropriate folder for the user to view wneh he/she decides to
>>open one of these documents.

I assume you are using 'GetSaveFileName()' ( for that purpose. In this case, all you need to do is setting 'lpstrInitialDir' in the associated OPENFILENAME struct to the directory where you want to save the file(s), e.g. like in the example at ("How to write a Windows XP Application that stores user and application data in the correct location by using Visual C++" - applies to later version of Windows as well)

void OnFileSaveAs(HWND hWnd)
   TCHAR szPath[MAX_PATH];
   TCHAR szFile[MAX_PATH];

    // Initialize OPENFILENAME structure.
    ZeroMemory( &openFile, sizeof(OPENFILENAME) );
    openFile.lStructSize = sizeof(OPENFILENAME);

    szFile[0] = '\0';
    openFile.hwndOwner = hWnd;
    openFile.lpstrFile = szFile;
    openFile.nMaxFile = sizeof(szFile)/sizeof(*szFile);

    // Default to My Pictures. First, get its path.
                                          NULL, 0, szPath ) ) )
       // Set lpstrInitialDir to the path that SHGetFolderPath obtains. 
       // This causes GetSaveFileName to point to the My Pictures folder.
          openFile.lpstrInitialDir = szPath;
    // Display the standard File Save dialog box, defaulting to My Pictures.
    if ( GetSaveFileName( &openFile ) == TRUE )
        // User clicks the Save button.
        // Save the file
       // User cancels the File Save dialog box.

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.