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

Posted on 2012-09-18
Last Modified: 2012-09-18
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.
Question by:Onager
    1 Comment
    LVL 86

    Accepted Solution

    >>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)
       OPENFILENAME openFile;
       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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now