Where should a Visual Studio Add-in store its settings?

前端 未结 2 902
我在风中等你
我在风中等你 2021-02-05 06:05

Currently I\'m storing the settings for my custom addins in the registry but this seems like a kludge. I was wondering if there was an official place to store add-in settings. M

2条回答
  •  天命终不由人
    2021-02-05 06:30

    Here is a quick tutorial that may give you an idea of how to achieve this simply (it is quite straightforward once you have done it once).

    I have derived the below code from what I use in my extensions; it is in VB.NET, but can be easily converted to C#.

    To start, simply add this class to your extension project. It should contain a property for each value you need to store. You can even arrange them in categories. You may have a look at MSDN here for supported types (for more complex cases you may refer to "customized option pages", which is a topic covered by MSDN here).

    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Diagnostics
    Imports System.Globalization
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel.Design
    Imports Microsoft.Win32
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Shell.Interop
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio.Shell
    Imports System.Threading
    Imports System.Text.RegularExpressions
    Imports System.ComponentModel
    
    
    
    Public Class OptionPageGrid
        Inherits DialogPage
    
        Private _MyBooleanSetting As Boolean = False
        
        
        
        Public Property MyBooleanSetting() As Boolean
            Get
                Return Me._MyBooleanSetting
            End Get
            Set(ByVal value As Boolean)
                Me._MyBooleanSetting = value
            End Set
        End Property
    
        Private _MyIntegerSetting As Integer = 2
        
        
        
        Public Property MyIntegerSetting() As Integer
            Get
                Return Me._MyIntegerSetting
            End Get
            Set(ByVal value As Integer)
                Me._MyIntegerSetting = value
            End Set
        End Property
    
        Private _MyStringSetting As String = "DefaultStringValue"
        
        
        
        Public Property MyStringSetting() As Integer
            Get
                Return Me._MyStringSetting
            End Get
            Set(ByVal value As Integer)
                Me._MyStringSetting = value
            End Set
        End Property
    End Class
    

    Then, add the following attributes just before your master package class.

    
    Public NotInheritable Class MyExtensionMainClass
        Inherits Package
    

    Now to easily access the settings, you may add the following property in your master package class:

    Protected ReadOnly Property Settings() As OptionPageGrid
        Get
            Return CType(GetDialogPage(GetType(OptionPageGrid)), OptionPageGrid)
        End Get
    End Property
    

    This makes it possible to access a value from anywhere in the class using a friendly:

    If (Me.Settings.MyBooleanSetting) Then MsgBox("It works!");
    

    Visual Studio will take care of persisting the settings, and they should be included when you use the import/export function (or any settings synchronization extension like this one).

提交回复
热议问题