2010-08-20 130 views
21

目前我正在我的自定义插件的设置存储在注册表中,但这看起来像一个kludge。我想知道是否有一个官方的地方来存储加载项设置。我最喜欢将它们存储在Visual Studio存储设置的位置,以便它们可以轻松导出和导入。Visual Studio加载项应该在哪里存储它的设置?

是否可以将附加设置存储在Visual Studio设置中,还是有更好的方法?

回答

8

编辑

我原来的答案,这个话题有一对夫妇的问题,我发现经过多年的使用它。我已经在下面列出了它的完整性,但这里是我对此的更新思路。

在VSIX中使用应用程序设置不是安全的。存储的设置文件路径的位置部分包括可执行文件的版本字符串和散列。当Visual Studio安装正式更新时,这些值将更改,并因此更改设置文件路径。 Visual Studio本身不支持使用应用程序设置,因此它不会尝试将此文件迁移到新位置,并且所有信息都会丢失。 支持的设置方法是WritableSettingsStore。这是通过SVsServiceProvider

public static WritableSettingsStore GetWritableSettingsStore(this SVsServiceProvider vsServiceProvider) 
{ 
    var shellSettingsManager = new ShellSettingsManager(vsServiceProvider); 
    return shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); 
} 

原来的答案

最直接的方法是使用NET的Application Settings基础设施来存储任何设置非常相似,应用程序设置和足够容易访问。这是一个成熟的框架,支持为您的项目添加设置基础设施。

但它不与Visual Studio的导入/导出设置基础结构集成。获得该工作是一个非常复杂的过程,其中包括登记自己作为一个VSPackage的,实施设置模式,等等。总的来说,我发现它真的不值得让跑的麻烦(没有成功)

+0

我可能会离开,因为这是我第一次尝试这样做,但我似乎能够使用您的新代码的唯一方法是将参数类型从SVsServiceProvider更改为IServiceProvider。 – jschroedl 2015-08-19 18:56:35

9

这里是一个快速教程这可能会让你知道如何简单地完成这件事(一旦你完成了一次,这很简单)。

我推导出我在my extensions中使用的代码如下:它在VB.NET中,但可以很容易地转换为C#。

要开始,只需将此类添加到您的扩展项目。它应该包含您需要存储的每个值的属性。你甚至可以安排他们的类别。对于支持的类型,您可能会看到at MSDN here(对于更复杂的情况,您可以参考“自定义选项页面”,这是涵盖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 

<ClassInterface(ClassInterfaceType.AutoDual)> 
<CLSCompliant(False), ComVisible(True)> 
Public Class OptionPageGrid 
    Inherits DialogPage 

    Private _MyBooleanSetting As Boolean = False 
    <Category("The name or an alias of my extension name")> 
    <DisplayName("Simple name of this setting displayed for the user")> 
    <Description("Longer description of this setting")> 
    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 
    <Category("The name or an alias of my extension name")> 
    <DisplayName("Simple name of this setting displayed for the user")> 
    <Description("Longer description of this setting")> 
    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" 
    <Category("The name or an alias of my extension name")> 
    <DisplayName("Simple name of this setting displayed for the user")> 
    <Description("Longer description of this setting")> 
    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 

然后,在您的主包类之前添加以下属性。

<ProvideOptionPage(GetType(OptionPageGrid), "The name or an alias of my extension name", "The name of a category of settings", 0, 0, True)> 
Public NotInheritable Class MyExtensionMainClass 
    Inherits Package 

现在可以轻松地访问设置,您可以在您的主包类中添加以下属性:

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

这可以从任何地方使用友好访问该类中的值:

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

Visual Studio将采取持续的设置照顾,他们应该被包含在您使用导入/导出功能(或任何设置同步等扩展this one)。

+0

感谢您的MSDN链接。我在C#中这样做,他们的样本以及你的样本使得它很容易启动和运行。我会注意到其他人,虽然你展示了如何创建网格选项页面,因为我想创建一个自定义的页面,因为我想让用户点击按钮。 – deadlydog 2014-02-21 22:22:27

+0

我已经博客了如何在C#中使用WPF用户控件为UI创建自定义选项页面,因为MSDN链接仅显示如何使用Windows窗体进行操作。 http://blog.danskingdom.com/adding-a-wpf-settings-page-to-the-tools-options-dialog-window-for-your-visual-studio-extension/ – deadlydog 2014-04-25 21:04:15

相关问题