chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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)
        {
 
        }
    }
}