Walkthrough: Creating a Settings Category (C#)
In this walkthrough, you create a Visual Studio settings category and use it to save values to and restore values from a settings file. A category is a group of related properties which appear as a "custom settings point", that is, as a check box in the Import and Export Settings Wizard. To start the wizard, on the Tools menu, click Import and Exports Settings. Settings are saved or restored as a category, and individual settings are not displayed in the wizard. For more information, see Visual Studio Settings.
The managed package framework (MPF) supports creating settings categories with very little additional code. You create a VSPackage to provide a container for the category by subclassing the Package class. You create the category itself by deriving it from the DialogPage class.
注意
Although DialogPage can provide a property grid or custom user interface (UI), neither is used by the settings manager.
To start this walkthrough you must first complete the first section of Walkthrough: Creating a Tools Options Page (C#). The resulting Tools Options property grid lets you examine and change the properties in the category. After you save the property category in a settings file, you examine the file to see how the property values are stored.
Creating a New Settings Category
In this section, you create a settings category by using the Visual Studio Integration Package Wizard. You use a custom settings point to save and restore the values of the settings category.
To create a new settings category |
---|
|
Examining the System Registry
In this section, you examine the system registry entries made by ProvideProfileAttribute.
To examine the system registry
Start the project in debug mode by pressing the keyboard shortcut, F5.
This starts Visual Studio Exp and writes the arguments of ProvideProfileAttribute to the system registry.
注意
Both versions of Visual Studio are open now.
Exit Visual Studio Exp.
Run the Registry Editor and examine the registry entry for HKLM\Software\Microsoft\VisualStudio\8.0Exp\UserSettings\MyCategory_MySettings. The following table includes subkeys of the entry although your GUIDs will differ:
Name
Data
(Default)
#107
Category
{ba1e23e3-fecc-425d-8259-06c40cfac1b6}
Description
#108
Package
{a2192704-7d66-44b7-b61b-44ed96aace98}
The default subkey value "#107" refers to the string resource "My Settings". The Description subkey value "#108" refers to the string resource "OptionInteger and OptionFloat".
Correcting the System Registry
In this section, you correct the system registry entries made by ProvideProfileAttribute.
ProvideProfileAttribute is designed to write resource IDs for unmanaged resources to the system registry. The Visual Studio resource loader expects numeric values preceded by "#" to be unmanaged resource IDs. Managed resources have no preceding "#". There is no workaround for this problem currently.
To correct the system registry
Correct the registry entry for HKLM\Software\Microsoft\VisualStudio\8.0Exp\UserSettings\MyCategory_MySettings by removing "#" from its numeric subkeys. The following table includes subkeys of the entry although your GUIDs will differ):
Name
Data
(Default)
107
Category
{ba1e23e3-fecc-425d-8259-06c40cfac1b6}
Description
108
Package
{a2192704-7d66-44b7-b61b-44ed96aace98}
ToolsOptionsPath
My Category
Right-click the registry entry and export it to the file "settings.reg" in the MyToolsOptions project folder.
Exit the Registry Editor.
Right-click the MyToolsOptions project node and add "settings.reg" to the project.
Open VsPkg.cs and comment out ProvideProfileAttribute and its arguments.
//[ProvideProfileAttribute(// typeof(OptionPageGrid), "My Category", "My Settings",// 106, 107, true, DescriptionResourceID = 108)]
This prevents the incorrect registry entries from being rewritten to the System Registry.
Right-click the MyToolsOptions project and then click Properties.
Click Build Events panel and add the following Post-build event command line. You must include the full path of the settings.reg file (your path may differ).
regedit /s D:\MyToolsOptions\MyToolsOptions\settings.reg
Build the MyToolsOptions solution. The corrected numeric subkeys will be written to the registry.
注意
You must also make these corrections when creating a registry file for final VSPackage deployment.
Examining the Settings File
In this section, you export property category values to a settings file. You examine the file and then import the values back into the property category.
To examine the settings file
Start the project in debug mode by pressing the keyboard shortcut, F5. This starts Visual Studio Exp.
注意
Both versions of Visual Studio are open now.
In Visual Studio Exp, on the Tools menu click Options.
The Options dialog box opens.
In the tree view in the left pane, expand My Category and then click My Grid Page.
The options grid appears in the right pane. The property category is "My Options", and the property names are OptionFloat and OptionInteger.
Change the value of OptionFloat to 3.1416 and OptionInteger to 12. Click OK.
On the Tools menu, click Import and Export Settings.
The Import and Export Settings wizard appears.
Make sure Export selected environment settings is selected, and then click Next.
The Choose Settings to Export page appears.
Click My Settings.
The Description changes to "OptionInteger and OptionFloat".
Make sure that the My Settings check box is checked, and then click Next.
The Name Your Settings File page appears.
Name the new settings file MySettings.vssettings and save it in an appropriate directory. Click Finish.
The Export Complete page reports that your settings were successfully exported.
On the File menu, point to Open, and then click File. Navigate to and open the MySettings.vssettings file.
You can find the property category you exported in this section of the file (your GUIDs will differ):
<Category name="My Category_My Settings" Category="{4802bc3e-3d9d-4591-8201-23d1a05216a6}" Package="{6bb6942e-014c-489e-a612-a935680f703d}" RegisteredName="My Category_My Settings"> PackageName="Company.MyToolsOptions.MyToolsOptions, MyToolsOptions, Version=1.0.2251.20398, Culture=neutral, PublicKeyToken=d74639816260e962"> <PropertyValue name="OptionFloat">3.1416</PropertyValue> <PropertyValue name="OptionInteger">12</PropertyValue> </Category>
Note that the full category name is formed by appending an underscore to the category name and then appending the object name to the underscore. OptionFloat and OptionInteger appear in the category, together with their exported values.
Close the settings file without changing it.
On the Tools menu, click Options, expand My Category, click My Grid Page and then change OptionFloat and OptionInteger to another value. Click OK.
On the Tools menu, click Import and Export Settings, choose Import selected environment settings, and then click Next.
The Save Current Settings page appears.
Choose No, just import new settings and then click Next.
The Choose a Collection of Settings to Import page appears.
Select the MySettings.vssettings file from the My Settings node of the tree view. If the file does not appear in the tree view, click Browse and find it. Click Next.
The Choose Settings to Import dialog appears.
Make sure that the My Settings check box is checked, click Finish, wait for the Import Complete page to appear, and then click Close.
On the Tools menu, click Options, expand My Category, click My Grid Page and verify that the property category values have been restored.