Select the product you need help with
How to write a Windows XP Application that stores user and application data in the correct location by using Visual C++Article ID: 310294 - View products that this article applies to. This article was previously published under Q310294 Important This article contains information about how to modify the registry. Make sure that you back up the registry before you modify it. Make sure that you know how to restore the registry if a problem occurs. For more information about how to back up, restore, and modify the registry, click the following article number to view the article in the Microsoft Knowledge Base: 322756 Note Microsoft Visual C++ .NET 2002, Microsoft Visual C++ .NET 2003, Microsoft Visual C++ 2005, and Microsoft Visual C++ 2008 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code
(http://support.microsoft.com/kb/322756/
)
How to back up and restore the registry in WindowsOn This PageSummary Applications work with two types of documents: those that
the user creates and those that the application creates. Your applications
should use the SHGetFolderPath shell function to retrieve valid folder locations to store data
that is specific to the user and the application. This is essential for Windows
XP applications to support multiple users who are using the same computer and
to enable users to switch quickly. This article describes how to store user data in the correct place in the following steps:
RequirementsThe following list outlines the recommended hardware, software, network infrastructure, skills, knowledge, and service packs that you need:
Create a Win32 ApplicationStart Visual Studio, and create a new Win32 application named SavingData.
Add a Save As Option to the File Menu
Use the standard File Save dialog box to default to the correct locationWhen a user displays an application's File Save (or File Open) dialog box for the first time, the dialog box must default to the user's My Documents folder (or a descendant of My Documents, such as My Pictures for image data and My Music for audio files).NOTE: You must never hard code a path within your application because you can never guarantee its physical location. For example, an Administrator may relocate the My Documents folder to a network location.
Verify the Correct File Save Location
Remember the User's Previous SelectionFor subsequent use of the File Save (or File Open) dialog box, it is recommended that the dialog box default to the user's previously selected location.If you do not supply an initial folder location within the OPENFILENAME structure, GetSaveFileName (and GetOpenFileName) display the standard File Save or File Open dialog box, which points to the My Documents folder. In addition, if the user has used one of these dialog boxes previously and has chosen a non-default folder, these functions automatically default to the previously used folder. To support the recommended best practice of targeting a specific folder location (such as My Pictures) the first time a user saves or loads a file, and to subsequently default to the user's previously selected location, you should use a Boolean variable to track whether this is the first time the user has performed the Save or Open operation.
Verify the User's Previous Selection
Classify Application DataYou should not store application-specific data (such as temporary files, user preferences, application configuration files, and so on) in the My Documents folder. Instead, use either an appropriate location in the Windows Registry (for data that does not exceed 64 kilobytes) or an application-specific file that is located in a valid Application Data folder.It is important to store application data in the correct location to allow several people to use the same computer without corrupting or overwriting each other's data and settings. To determine the most appropriate location for your application data, use the following categories to classify your data:
Store Application Data in the Correct LocationYou use the SHGetFolderPath function to retrieve the correct Application Data folder. Do not store application data directly in this folder. Instead, use the PathAppend function to append a subfolder to the path that SHGetFolderPath returns. Make sure that you use the following convention:
Company Name\Product Name\Product Version
For example, the resultant full path may appear as follows:
\Documents and Settings\All Users\Application Data\My Company\My Product\1.0
To locate the correct Application Data folder, pass the
appropriate CSIDL value, based on the category of your application data.
Use the Registry JudiciouslyImportant This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:322756
(http://support.microsoft.com/kb/322756/
)
How to back up and restore the registry in WindowsYou can also use the registry to store small amounts of application data. For data that exceeds 64 kilobytes (KB), you must use an Application Data folder. Observe the following guidelines when you use the registry to store application data:
Troubleshooting
References For other top-hit Visual C++ .NET Microsoft Knowledge Base
articles, visit the Visual C++ .NET Support Center at the following Microsoft
Web site:
Visual C++ .NET (2002) Support Center For more information about the complete set of folders that SHGetFolderPath can identify, see the following Microsoft Platform Software
Development Kit (SDK) documentation: http://support.microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet
(http://support.microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet)
CSIDL For more information about shell programming in general, see the
following Microsoft Developer Network (MSDN) Web site: http://msdn.microsoft.com/en-us/library/bb762494.aspx
(http://msdn.microsoft.com/en-us/library/bb762494.aspx)
Shell Programmers Guide http://msdn2.microsoft.com/en-us/library/bb776778.aspx
(http://msdn2.microsoft.com/en-us/library/bb776778.aspx)
PropertiesArticle ID: 310294 - Last Review: October 16, 2012 - Revision: 4.0 Applies to
| Article Translations
|


Back to the top








