Visual Studio 外接程序应在哪里存储其设置?

发布于 2024-09-15 12:23:19 字数 163 浏览 2 评论 0原文

目前,我将自定义插件的设置存储在注册表中,但这看起来像是一个拼凑。我想知道是否有一个官方的地方来存储加载项设置。我的偏好是将它们存储在 Visual Studio 存储设置的位置,以便可以轻松导出和导入它们。

是否可以使用 Visual Studio 设置来存储加载项设置,或者是否有更好的方法?

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. My preference would be to store them where Visual studio stores settings so they can be exported and imported easily.

Is it possible to store add-in settings with Visual Studio settings or is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-09-22 12:23:19

编辑

我对这个主题的最初回答有几个问题,我在使用它多年后发现了一些问题。为了完整起见,我将其包含在下面,但这是我对此的最新想法。

在 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 的 应用程序设置 基础结构,用于存储任何设置。它是一个成熟的框架,具有设计人员的支持,可将设置基础架构添加到您的项目中。

但是,它不与 Visual Studio 的导入/导出设置基础结构集成。让它工作是一个非常复杂的过程,包括将自己注册为 VSPackage、实现设置架构等...我通常发现它确实不值得运行(从未成功)

EDIT

My original answer to this topic had a couple of issues I discovered after years of using it. I've included it below for completeness but here is my updated thoughts on this.

The use of application settings is not version safe in a VSIX. The location of the stored setting file path in part includes the version string and hashes of the executable. When Visual Studio installs an official update these values change and as a consequence change the setting file path. Visual Studio itself doesn't support the use of application settings hence it makes no attempt to migrate this file to the new location and all information is essentially lost.
The supported method of settings is the WritableSettingsStore. It's very similar to application settings and easy enough to access via SVsServiceProvider

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

Original Answer

The most straight forward way is to use .Net's Application Settings infrastructure to store any settings. It's a mature framework with designer support for adding a settings infrastructure to your project.

It however does not integrate with Visual Studio's Import / Export settings infrastructure. Getting that working is a very involved process that includes registering yourself as a VSPackage, implementing a settings schema, etc ... I generally find it's really not worth the trouble of getting running (never succeeded)

熟人话多 2024-09-22 12:23:19

这是一个快速教程,可以让您了解如何简单地实现这一目标(一旦您完成了一次,它就非常简单)。

我从我的扩展中使用的代码派生出以下代码;它是用 VB.NET 编写的,但可以轻松转换为 C#。

首先,只需将此类添加到您的扩展项目中即可。它应该包含您需要存储的每个值的属性。您甚至可以将它们分类。您可以在 MSDN 上查看支持的类型(了解更多对于复杂的情况,您可以参考“自定义选项页面”,该主题涵盖 MSDN 此处)。

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 将负责保留设置,并且它们应该是当您使用导入/导出功能(或任何设置同步扩展,例如 这个)。

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

<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

Then, add the following attributes just before your master package 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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文