using Microsoft.Win32; using System; using System.Collections.Generic; namespace VersionControl { public static class VersionUtil { public const string APPID_KEY = "AppId"; public const string VERSION_KEY = "Version"; /// /// 从注册表获取插件的当前版本 /// /// 注册表中存在的版本 /// public static Dictionary GetRegPluginVersion() { RegistryKey hklm = Registry.CurrentUser; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\SwPlugin", true); if (hkSoftWare == null) { throw new NoSoftwareKeyException("No Software Version Key."); } string appId = hkSoftWare.GetValue(APPID_KEY).ToString(); string version = hkSoftWare.GetValue(VERSION_KEY).ToString(); hklm.Close(); hkSoftWare.Close(); return new Dictionary { { APPID_KEY,appId}, { VERSION_KEY,version} }; } public static void SetPluginVersion(string appId, string version) { try { RegistryKey hklm = Registry.CurrentUser; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\SwPlugin", true); if (hkSoftWare == null) { hkSoftWare = hklm.CreateSubKey(@"SOFTWARE\SwPlugin"); } hkSoftWare.SetValue(APPID_KEY, appId); hkSoftWare.SetValue(VERSION_KEY, version); hklm.Close(); hkSoftWare.Close(); } catch (Exception ex) { throw new VersionWriteException("Version write Reg failed.", ex); } } } /// /// 没找到版本注册表的异常 /// public class NoSoftwareKeyException : Exception { public NoSoftwareKeyException() { } public NoSoftwareKeyException(string message) : base(message) { } } /// /// 版本写入注册表失败异常 /// public class VersionWriteException : Exception { public VersionWriteException() { } public VersionWriteException(string message) : base(message) { } public VersionWriteException(string message, Exception ex) : base(message, ex) { } } }