注册表项getvalue未获取最新值

时间:2016-09-13 20:05:36

标签: c#

我试图通过从我的应用程序中读取string KeyboardLayoutRegKey = "Keyboard Layout\\Preload"; pLastUsedKeyboard = RegistryAccessor.CurrentUser.OpenSubKey(KeyboardLayoutRegKey).GetValue("1").ToString(), 来获取当前用户最后使用的键盘布局。每次在控制面板中更改默认键盘时,我都会看到上述位置的值发生变化(我可以通过打开RegEdit来看到这一点)。但由于某种原因,我的应用程序仍然会读取旧值,而不会在RegEdit中看到最新的刷新帐号。

/// <summary>
/// Provides access to the registry. 
/// </summary>
public class RegistryAccessor : IRegistryAccessor
{
    /// <summary>
    /// Contains information about the current user preferences. This field reads the Windows registry base key HKEY_CURRENT_USER.
    /// </summary>
    public IRegistryKey CurrentUser
    {
        get
        {
            return new RegistryKeyWrapper(Registry.CurrentUser);
        }
    }

    /// <summary>
    /// Contains the configuration data for the local machine. This field reads the Windows registry base key HKEY_LOCAL_MACHINE.
    /// </summary>
    public IRegistryKey LocalMachine
    {
        get
        {
            return new RegistryKeyWrapper(Registry.LocalMachine);
        }
    }
    /// <summary>
    /// Contains the configuration data for the Users. This field reads the Windows registry base key HKEY_USERS.
    /// </summary>
    public IRegistryKey Users
    {
        get
        {
            return new RegistryKeyWrapper(Registry.Users);
        }
    }

}

当我将值从英语 - 美国更改为法语时,注册表中的十六进制值从409更改为40c,我可以在RegEdit中看到,但我的应用程序仍然将其读取为409,直到我重新启动?为什么这样做?我的应用程序针对AnyCPU。

修改

AnySorter

1 个答案:

答案 0 :(得分:0)

如果不重新启动应用程序,则无法“刷新”注册表值。如果在应用程序运行时为了更改注册表项值而需要获得通知,则可以使用WMI实现RegistryValueChangeEvent。您可以参考可以帮助您在应用程序中实现它的this answer