OSDN Git Service

3ba1ff41592be3de84b551d795296dff2f78d5cc
[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 static class SystemProxy\r
24     {\r
25         private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";\r
26         private const string _regName = "AutoConfigURL";\r
27 \r
28         public static string AutoConfigUrl\r
29         {\r
30             get\r
31             {\r
32                 using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath))\r
33                 {\r
34                     if (regkey == null)\r
35                         return "";\r
36                     return regkey.GetValue(_regName) as string ?? "";\r
37                 }\r
38             }\r
39             set\r
40             {\r
41                 using (var regkey = Registry.CurrentUser.OpenSubKey(_regPath, true))\r
42                 {\r
43                     if (regkey == null)\r
44                         return;\r
45                     if (value == "")\r
46                     {\r
47                         regkey.DeleteValue(_regName, false);\r
48                     }\r
49                     else\r
50                     {\r
51                         regkey.SetValue(_regName, value);\r
52                     }\r
53                 }\r
54                 Refresh();\r
55             }\r
56         }\r
57 \r
58         /// <summary>\r
59         /// PACファイルでDIRECTを指定すると、すべてのサイトがローカルイントラネットになり、\r
60         /// IEが互換表示になるなどの不具合があるので、イントラネットにならないようにする\r
61         /// </summary>\r
62         public static void AdjustLocalIntranetZoneFlags()\r
63         {\r
64             var zones = Registry.CurrentUser.OpenSubKey(_regPath + @"\Zones\1", true);\r
65             if (zones == null)\r
66                 return;\r
67             if (!(zones.GetValue("Flags") is int flags))\r
68                 return;\r
69             zones.SetValue("Flags", flags & (-1 ^ 0x108));\r
70         }\r
71 \r
72         public static void Refresh()\r
73         {\r
74             InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);\r
75             InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);\r
76         }\r
77 \r
78         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]\r
79         private static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption,\r
80             IntPtr lpBuffer, int dwBufferLength);\r
81 \r
82         private enum InternetOption : uint\r
83         {\r
84             INTERNET_OPTION_REFRESH = 37,\r
85             INTERNET_OPTION_SETTINGS_CHANGED = 39\r
86         }\r
87     }\r
88 }