[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

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.
0
Onager
Asked:
Onager
1 Solution
 
jkrCommented:
>>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()' (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646928%28v=vs.85%29.aspx) 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 http://support.microsoft.com/kb/310294 ("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.
    if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_MYPICTURES, 
                                          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
    }
    else
    {
       // User cancels the File Save dialog box.
    }
}

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now