这些VB.NET方法是否更快:SaveSetting()或SetValue()?

时间:2019-06-28 23:57:59

标签: vb.net registry

我在注册表中大量更新了我的应用程序的指标,并想知道哪种写入系统注册表的方法明显更快。

1 个答案:

答案 0 :(得分:0)

如果您使用.NET反编译器来查看Interaction名称空间下的Microsoft.VisualBasic类/模块的源代码,则会发现SaveSetting()方法实际上使用了{ {1}}: *

SetValue()

或者在VB中,如果您愿意:

/// <summary>Saves or creates an application entry in the Windows registry. The My feature gives you greater productivity and performance in registry operations than SaveSetting. For more information, see <see cref="P:Microsoft.VisualBasic.Devices.ServerComputer.Registry" />.</summary>
/// <param name="AppName">Required. String expression containing the name of the application or project to which the setting applies.</param>
/// <param name="Section">Required. String expression containing the name of the section in which the key setting is being saved.</param>
/// <param name="Key">Required. String expression containing the name of the key setting being saved.</param>
/// <param name="Setting">Required. Expression containing the value to which <paramref name="Key" /> is being set.</param>
/// <exception cref="T:System.ArgumentException">Key registry could not be created, or user is not logged in.</exception>
/// <filterpriority>1</filterpriority>
/// <PermissionSet>
///   <IPermission class="System.Security.Permissions.RegistryPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
public static void SaveSetting(string AppName, string Section, string Key, string Setting)
{
    Interaction.CheckPathComponent(AppName);
    Interaction.CheckPathComponent(Section);
    Interaction.CheckPathComponent(Key);
    string text = Interaction.FormRegKey(AppName, Section);
    RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(text);
    if (registryKey == null)
    {
        throw new ArgumentException(Utils.GetResourceString("Interaction_ResKeyNotCreated1", new string[]
        {
            text
        }));
    }
    try
    {
        registryKey.SetValue(Key, Setting);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        registryKey.Close();
    }
}

因此,我希望它们在这种特殊情况下具有相同的性能(假设您基本上将复制相同的行为)。但是,Microsoft.Win32.Registry类(或My.Computer.Registry属性,只是该类的包装器)将变得更加灵活,并提供了更多选项。文档(thanks, Jimi!)和上面显示的XML注释中。

关于性能,要100%确定(假设您确实需要),则应该始终进行测量。


* 此代码是使用ILSpy提取的。