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";
|
|
/// <summary>
|
/// 从注册表获取插件的当前版本
|
/// </summary>
|
/// <returns>注册表中存在的版本</returns>
|
/// <exception cref="NoSoftwareKeyException"></exception>
|
public static Dictionary<string, string> 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<string, string> {
|
{ 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);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 没找到版本注册表的异常
|
/// </summary>
|
public class NoSoftwareKeyException : Exception
|
{
|
public NoSoftwareKeyException()
|
{
|
|
}
|
|
public NoSoftwareKeyException(string message) : base(message)
|
{
|
|
}
|
}
|
|
/// <summary>
|
/// 版本写入注册表失败异常
|
/// </summary>
|
public class VersionWriteException : Exception
|
{
|
public VersionWriteException()
|
{
|
|
}
|
|
public VersionWriteException(string message) : base(message)
|
{
|
|
}
|
|
public VersionWriteException(string message, Exception ex) : base(message, ex)
|
{
|
|
}
|
}
|
}
|