Home > Code Samples > How to read/write different DXCore setting files from your own plug-in

How to read/write different DXCore setting files from your own plug-in

April 22nd, 2011

Sometimes, when developing a DXCore plug-in, you might need to know what settings other CodeRush or Refactor! features have. For example, it might be necessary to know if the particular feature is enabled or disabled to not intersect with it; or change a feature’s default settings from inside your own plug-in. This way, you can tweak any settings programmatically without opening the Options Dialog.

DXCore shipped with several default settings files. However, several plug-ins don’t have settings files, and use default hard-coded values. The settings file for such plug-ins may not exist until you change something on the plug-in’s Options page. The settings file is created when you save feature’s options inside the Options Dialog.

If the settings file exists, you can access it for reading or writing, using the DXCore built-in DecoupledStorage object, which is dedicated to specially handle DXCore plug-ins options storage. First of all, you need to know the path to the storage (Category), its name (Page Name) and language in which these settings are stored. See the Structure of the DecoupledStorage topic to learn more.

Imagine we would to like to know what programming languages DXCore knows. The settings file, where this information is stored, doesn’t have a corresponding options page, it is stored inside %SettingsPath%\Core\KnownLanguages.xml file. The content of this file may look similar to this:

DXCore Known Languages settings file

To work with this file, we are going to create a new DecoupledStorage object, and specify the location of the file:

DecoupledStorage storage = new DecoupledStorage(@"Core", @"KnownLanguages");

Show Visual Basic code… »

Dim storage As DecoupledStorage = New DecoupledStorage("Core", "KnownLanguages")

Now we can read or change the content of the storage, if necessary. The following function returns the list of all programming languages stored inside the settings file:

private List<string> LoadKnownLanguages()
  List<string> languagesList = new List<string>();
  using (DecoupledStorage storage = new DecoupledStorage(@"Core", @"KnownLanguages"))
    int languagesCount = storage.ReadInt32("Known Languages", "Count");
    for (int i = 0; i < languagesCount; i++)
      string languageName = storage.ReadString("Known Languages", "Item" + i);
  return languagesList;

Show Visual Basic code… »

Private Function LoadKnownLanguages() As List(Of String)
  Dim languagesList As List(Of String) = New List(Of String)()
  Using storage As DecoupledStorage = New DecoupledStorage("Core", "KnownLanguages")
    Dim languagesCount As Integer = storage.ReadInt32("Known Languages", "Count")
    Dim i As Integer = 0
    While i < languagesCount
      Dim languageName As String = storage.ReadString("Known Languages", "Item" + i.ToString())
      i += 1
    End While
  End Using
  Return languagesList
End Function

The sample plug-in is attached (43938 bytes, Visual Studio 2010. It shows the message box with the results of reading the settings file:

Reading DXCore Known Languages settings file result

In contrast to usual plug-ins settings, CodeRush templates are serialized, using a different method. Read the appropriate topic, to learn more on how to load them.

Products: DXCore
Versions: 10.2 and up
VS IDEs: any
Updated: May/19/2011
ID: D076

Similar Posts: