简体   繁体   English

在VSTO插件中存储某些数据的最佳方法是什么?

[英]What is the best way to store some data in VSTO addin?

I have developed one outlook add-in, that has to be On or Off. 我已经开发了一个Outlook加载项,必须为On或Off。

to do that i have declared one static variable as shown below, 为此,我声明了一个静态变量,如下所示,

ThisAddIn.cs ThisAddIn.cs

public static bool isAddInOn = false;

RibbonButton.cs RibbonButton.cs

    private void btnRibbon_Click(object sender, RibbonControlEventArgs e)
    {
        if (ThisAddIn.isAddInOn )
        {

            ThisAddIn.isAddInOn = false; 
            btnRibbon.Label = "Disabled";

        }
        else
        {

            ThisAddIn.isAddInOn = true;
            btnRibbon.Label = "Enabled";


        }
    }

It is working. 这是工作。 But the static variable reset again when i close outlook and open it again. 但是当我关闭Outlook并再次打开它时,静态变量会再次重置。 That means when i open outlook by default my add-in is in disabled state. 这意味着当我默认打开Outlook时,我的加载项处于禁用状态。

I just want to store that value at some place. 我只想在某个地方存储该值。 so i can check that value when outlook reopened. 这样我可以在Outlook重新打开时检查该值。

Scenario: 场景:

1) open outlook 1)展望未来

2) Enable add-in by clicking on its logo (that is in ribbon) 2)通过单击其徽标(在功能区中)启用加载项

3) now close the outlook 3)现在关闭前景

4) when i open outlook again it must enabled 4)当我再次打开Outlook时,它必须启用

so how can i achieve this ? 那我该怎么做到呢?

There are several techniques you can use to achieve this result. 您可以使用多种技术来获得此结果。 For sure your settings must be serialized to some storage/deserialized during startup of add-in. 确保在外接程序启动期间必须将您的设置序列化为一些存储/反序列化。

One of possible solution could be to use registry for that (in this case probably HKCU (Current user, then it will be private for each user using your add-in) and no special permission is needed. 一种可能的解决方案是为此使用注册表(在这种情况下,可能是HKCU(当前用户,然后它将使用您的外接程序供每个用户使用),并且不需要任何特殊权限。

For storing variable: 对于存储变量:

    public void StoreInRegistry(string keyName, string value)
    {
        RegistryKey rootKey = Registry.CurrentUser;
        string registryPath = @"Software\YourCompanyName\YourAddInName";
        using (RegistryKey rk = rootKey.CreateSubKey(registryPath))
        {
            rk.SetValue(keyName, value, RegistryValueKind.String);
        }
    }

For reading variable: 对于读取变量:

    public string ReadFromRegistry(string keyName, string defaultValue)
    {
        RegistryKey rootKey = Registry.CurrentUser;
        string registryPath = @"Software\YourCompanyName\YourAddInName";
        using (RegistryKey rk = rootKey.OpenSubKey(registryPath, false))
        {
            if (rk == null)
            {
                return defaultValue;
            }

            var res = rk.GetValue(keyName, defaultValue);
            if (res == null)
            {
                return defaultValue;
            }

            return res.ToString();
        }
    }

Such stored/retrieved variable should be used during add-in initialization to set your properties. 此类存储/检索变量应在加载项初始化期间用于设置属性。 So modification could look like: 因此修改看起来像:

ThisAddin.cs ThisAddin.cs

public static bool isAddInOn = ReadFromRegistry("MySetting1", "0") == "1";

RibbonButton.cs RibbonButton.cs

private void btnRibbon_Click(object sender, RibbonControlEventArgs e)
{
    if (ThisAddIn.isAddInOn )
    {

        ThisAddIn.isAddInOn = false; 
        btnRibbon.Label = "Disabled";

    }
    else
    {

        ThisAddIn.isAddInOn = true;
        btnRibbon.Label = "Enabled";


    }
    StoreInRegistry("MySetting1", ThisAddIn.isAddInOn ? "1" : "0");
}

Other options could serialization to file - some class with settings serialized to ie isolated storage, database (local or central) etc. 其他选项可以序列化到文件-一些类的设置序列化到隔离存储,数据库(本地或中央)等。

I've used several methods over the years to store configuration data for users. 多年来,我已经使用了几种方法来存储用户的配置数据。

  • Properties.Settings.Default.Properties, so writing in the application project properties. Properties.Settings.Default.Properties,因此在应用程序项目中编写属性。 It's solid, never had an issue with it, for hundreds of users over several years. 多年来,对于数百名用户来说,它是可靠的,从未有过任何问题。

  • Local config files in text, so writing to a known area for the user with fallback. 本地配置文件(以文本形式),因此使用后备写入用户的已知区域。 In a stable environment, one can choose the home area for the user, and read/write to the local config file, which also makes it accessible to support if it breaks and needs manual changes. 在稳定的环境中,可以为用户选择家乡区域,并读取/写入本地配置文件,这也使它可以在需要中断和需要手动更改时进行支持。 As a fallback, one could write to the local temp folder. 作为后备,可以写到本地temp文件夹。

  • Registry is an option i have not used in this case, but it is likely to be a good choice. 在这种情况下,我没有使用过注册表选项,但它可能是一个不错的选择。

Performance is likely a key concern considering it will impact the UI for users. 考虑到性能会影响用户界面,性能可能是一个关键问题。 Another concern is ease of use for the developer. 另一个问题是开发人员的易用性。 For both, my choice would be setting it in the application's properties, where reading and writing is very simple and handled within code, and likely very fast. 对于这两者,我的选择都是在应用程序的属性中进行设置,在该属性中,读取和写入非常简单并且可以在代码中进行处理,并且可能非常快。

Write

Properties.Settings.Default.PropertyName = propertValue;

Read

var propertValue = Properties.Settings.Default.PropertyName;

Settings can be stored as a hidden (associated) item in a folder, such as the Inbox or the Calendar folder. 设置可以作为隐藏(关联)项目存储在文件夹中,例如“收件箱”或“日历”文件夹。 For example, Outlook stores the list of categories as a hidden item in the Calendar folder. 例如,Outlook将类别列表存储为Calendar文件夹中的隐藏项目。 POP3 message ids are stored in a hidden item in the Inbox. POP3邮件ID存储在“收件箱”中的隐藏项目中。 The advantage of the hidden items is the roaming capability - Exchange mailbox user can see the data from any computer. 隐藏项目的优点是漫游功能-Exchange邮箱用户可以从任何计算机上查看数据。

You can see the hidden items in OutlookSpy - click IMAPIFolder button, go to the "Associated Contents" tab. 您可以在OutlookSpy中看到隐藏的项目-单击IMAPIFolder按钮,转到“相关内容”选项卡。

Programmatically, such items can be accessed using MAPIFolder.GetStorage in the Outlook Object Model. 通过编程,可以使用Outlook对象模型中的MAPIFolder.GetStorage访问此类项目。

2018 UPDATED ANSWER 2018年更新的答案

The recommended way to achieve this is now to use the already configured settings files in your project's properties. 现在推荐的方法是在项目属性中使用已经配置的设置文件。 These files are auto-generated when create your project : 这些文件是在创建项目时自动生成的:

在此处输入图片说明

And open the following window when clicked : 并在单击时打开以下窗口:

在此处输入图片说明

You can access your settings value programmatically into Properties.Settings.Default.Properties anywhere. 您可以通过编程方式在任何位置访问Properties.Settings.Default.Properties设置值。

The header bar at the top of the Settings page contains several controls: “设置”页面顶部的标题栏包含多个控件:

Synchronize 同步

Synchronize restores user-scoped settings that the application uses at run time or during debugging to their default values as defined at design time. 同步将应用程序在运行时或调试期间使用的用户作用域设置恢复为设计时定义的默认值。 To restore the data, remove run-time generated application-specific files from disk, not from project data. 要还原数据,请从磁盘而不是项目数据中删除运行时生成的应用程序特定文件。

Load Web Settings 加载网页设置

Load Web Settings displays a Login dialog box that enables you to load settings for an authenticated user or for anonymous users. “加载Web设置”显示一个“登录”对话框,通过该对话框,您可以加载经过身份验证的用户或匿名用户的设置。 This button is enabled only when you've enabled client application services on the Services page and specified a Web settings service location. 仅当您在“服务”页面上启用了客户端应用程序服务并指定了Web设置服务位置时,才启用此按钮。

View Code 查看代码

For C# projects, the View Code button enables you to view the code in the Settings.cs file . 对于C#项目,使用“查看代码”按钮可以查看Settings.cs file的代码。 This file defines the Settings class, which enables you to handle specific events on the Settings object. 该文件定义了Settings类,使您可以处理Settings对象上的特定事件。 In languages other than Visual Basic, you must explicitly call the Save method of this wrapper class in order to persist the user settings. 在Visual Basic以外的其他语言中,您必须显式调用此包装器类的Save方法,以保留用户设置。 You usually do this in the Closing event handler of the main form. 通常,您可以在主窗体的Closing事件处理程序中执行此操作。 Following is an example of a call to the Save method: C# 以下是调用Save方法的示例:C#

Properties.Settings.Default.Save();

For Visual Basic projects, the View Code button enables you to view the code in the Settings.vb file. 对于Visual Basic项目,使用“查看代码”按钮可以查看Settings.vb文件中的代码。 This file defines the MySettings class, which enables you to handle specific events on the My.Settings object. 该文件定义MySettings类,使您可以处理My.Settings对象上的特定事件。 For more information about accessing application settings by using the My.Settings object, see Access application settings. 有关使用My.Settings对象访问应用程序设置的更多信息,请参见访问应用程序设置。

For more information about accessing application settings, see Application settings for Windows Forms. 有关访问应用程序设置的更多信息,请参见Windows窗体的应用程序设置。

Access modifier 访问修饰符

The Access modifier button specifies the access level of the Properties.Settings (in C#) or My.Settings (in Visual Basic) helper classes that Visual Studio generates in Settings.Designer.cs or Settings.Designer.vb . 访问修饰符按钮指定Visual Studio在Settings.Designer.csSettings.Designer.vb生成的Properties.Settings(在C#中)或My.Settings (在Visual Basic中)帮助程序类的访问级别。

For Visual C# projects, the access modifier can be Internal or Public. 对于Visual C#项目,访问修饰符可以是Internal或Public。

For Visual Basic projects, the access modifier can be Friend or Public. 对于Visual Basic项目,访问修饰符可以是Friend或Public。

By default, the setting is Internal in C# and Friend in Visual Basic. 默认情况下,该设置在C#中为Internal,在Visual Basic中为Friend。 When Visual Studio generates helper classes as Internal or Friend, executable (.exe) applications cannot access the resources and settings that you have added to class libraries (.dll files). 当Visual Studio生成帮助程序类为Internal或Friend时,可执行(.exe)应用程序无法访问您添加到类库(.dll文件)的资源和设置。 If you have to share resources and settings from a class library, set the access modifier to Public. 如果必须共享类库中的资源和设置,请将访问修饰符设置为“公共”。

For more information about the settings helper classes, see Manage application settings. 有关设置帮助程序类的更多信息,请参阅管理应用程序设置。

Settings grid 设置网格

Settings Grid is used to configure application settings. 设置网格用于配置应用程序设置。 This grid includes the following columns: 该网格包括以下列:

Name 名称

Enter the name of the application setting in this field. 在此字段中输入应用程序设置的名称。

Type 类型

Use the drop-down list to select a type for the setting. 使用下拉列表选择设置的类型。 The most frequently used types appear in the drop-down list, for example, String, (Connection string), and System.Drawing.Font. 最常用的类型出现在下拉列表中,例如,String,(连接字符串)和System.Drawing.Font。 You can choose another type by selecting Browse at the end of the list, and then selecting a type from the Select a Type dialog box. 您可以通过选择列表末尾的“浏览”来选择其他类型,然后从“选择类型”对话框中选择一种类型。 After you choose a type, it's added to the common types in the drop-down list (for the current solution only). 选择类型后,将其添加到下拉列表的常用类型中(仅适用于当前解决方案)。

Scope 范围

Select either Application or User. 选择应用程序或用户。

Application-scoped settings, such as connection strings, are associated with the application. 应用程序范围的设置(例如连接字符串)与应用程序相关联。 Users can't change application-scoped settings at run time. 用户无法在运行时更改应用程序范围的设置。

User-scoped settings, such as system fonts, are intended to be used for user preferences. 用户范围的设置(例如系统字体)旨在用于用户首选项。 Users can change them at run time. 用户可以在运行时更改它们。

Value

The data or value associated with the application setting. 与应用程序设置关联的数据或值。 For example, if the setting is a font, its value could be Verdana, 9.75pt, style=Bold . 例如,如果设置是字体,则其值可以是Verdana, 9.75pt, style=Bold

Documentation link 文档链接

Reading settings 阅读设定

Writing settings 书写设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM