1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Runtime.InteropServices;
\r
17 using Microsoft.Win32;
\r
19 // ReSharper disable InconsistentNaming
\r
21 namespace KancolleSniffer
\r
23 public class SystemProxy
\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
29 private void SaveSettings()
\r
31 if (_prevUrl != null)
\r
33 using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath))
\r
34 _prevUrl = regkey?.GetValue(_regName) as string ?? "delete";
\r
37 public void SetAutoProxyUrl(string url)
\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
47 public void RestoreSettings()
\r
49 if (_prevUrl == null)
\r
51 using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath, true))
\r
53 if (_prevUrl == "delete")
\r
55 regkey?.DeleteValue(_regName, false);
\r
59 regkey?.SetValue(_regName, _prevUrl);
\r
65 public static void Refresh()
\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
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
75 private enum InternetOption : uint
\r
77 INTERNET_OPTION_REFRESH = 37,
\r
78 INTERNET_OPTION_SETTINGS_CHANGED = 39
\r