Visual Studio 外接程序应在哪里存储其设置?
目前,我将自定义插件的设置存储在注册表中,但这看起来像是一个拼凑。我想知道是否有一个官方的地方来存储加载项设置。我的偏好是将它们存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
我对这个主题的最初回答有几个问题,我在使用它多年后发现了一些问题。为了完整起见,我将其包含在下面,但这是我对此的最新想法。
在 VSIX 中使用应用程序设置不是版本安全的。存储的设置文件路径的位置部分包括可执行文件的版本字符串和哈希值。当 Visual Studio 安装官方更新时,这些值会发生变化,从而更改设置文件路径。 Visual Studio 本身不支持使用应用程序设置,因此它不会尝试将此文件迁移到新位置,并且所有信息基本上都会丢失。
支持的设置方法是 WritableSettingsStore。它与应用程序设置非常相似,并且很容易通过 SVsServiceProvider 进行访问
原始答案
最直接的方法是使用 .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
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)
这是一个快速教程,可以让您了解如何简单地实现这一目标(一旦您完成了一次,它就非常简单)。
我从我的扩展中使用的代码派生出以下代码;它是用 VB.NET 编写的,但可以轻松转换为 C#。
首先,只需将此类添加到您的扩展项目中即可。它应该包含您需要存储的每个值的属性。您甚至可以将它们分类。您可以在 MSDN 上查看支持的类型(了解更多对于复杂的情况,您可以参考“自定义选项页面”,该主题涵盖 MSDN 此处)。
然后,在主包类之前添加以下属性。
现在,为了轻松访问设置,您可以在主包类中添加以下属性:
这使得可以使用友好的方式从类中的任何位置访问值:
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).
Then, add the following attributes just before your master package class.
Now to easily access the settings, you may add the following property in your master package class:
This makes it possible to access a value from anywhere in the class using a friendly:
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).