OSDN Git Service

プロキシの設定をレジストリ経由で行う
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / SystemProxy.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Runtime.InteropServices;\r
17 using Microsoft.Win32;\r
18 \r
19 // ReSharper disable InconsistentNaming\r
20 \r
21 namespace KancolleSniffer\r
22 {\r
23     public class SystemProxy\r
24     {\r
25         private string _prevUrl;\r
26         private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";\r
27         private const string _regName = "AutoConfigURL";\r
28 \r
29         private void SaveSettings()\r
30         {\r
31             if (_prevUrl != null)\r
32                 return;\r
33             using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath))\r
34                 _prevUrl = regkey?.GetValue(_regName) as string ?? "delete";\r
35         }\r
36 \r
37         public void SetAutoProxyUrl(string url)\r
38         {\r
39             SaveSettings();\r
40             using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath, true))\r
41                 regkey?.SetValue(_regName, url);\r
42             if (_prevUrl == url) // 同じURLの場合は終了時に設定を消す\r
43                 _prevUrl = "delete";\r
44             Refresh();\r
45         }\r
46 \r
47         public void RestoreSettings()\r
48         {\r
49             if (_prevUrl == null)\r
50                 return;\r
51             using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath, true))\r
52             {\r
53                 if (_prevUrl == "delete")\r
54                 {\r
55                     regkey?.DeleteValue(_regName, false);\r
56                 }\r
57                 else\r
58                 {\r
59                     regkey?.SetValue(_regName, _prevUrl);\r
60                 }\r
61             }\r
62             Refresh();\r
63         }\r
64 \r
65         public static void Refresh()\r
66         {\r
67             InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);\r
68             InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);\r
69         }\r
70 \r
71         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]\r
72         private static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption,\r
73             IntPtr lpBuffer, int dwBufferLength);\r
74 \r
75         private enum InternetOption : uint\r
76         {\r
77             INTERNET_OPTION_REFRESH = 37,\r
78             INTERNET_OPTION_SETTINGS_CHANGED = 39\r
79         }\r
80     }\r
81 }