44using System . Text ;
55using System . Text . RegularExpressions ;
66
7- namespace SetProxy
7+ namespace UsingDotNET . SetProxy ;
8+
9+ public partial class Form1 : Form
810{
9- public partial class Form1 : Form
11+ [ DllImport ( "wininet.dll" ) ]
12+ public static extern bool InternetSetOption ( nint hInternet , int dwOption , nint lpBuffer , int dwBufferLength ) ;
13+
14+ public const int InternetOptionSettingsChanged = 39 ;
15+ public const int InternetOptionRefresh = 37 ;
16+ private const string UserRoot = "HKEY_CURRENT_USER" ;
17+ private const string Subkey = "Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Internet Settings" ;
18+ private static string _keyName = "" ;
19+ private const string ProxyEnable = "ProxyEnable" ;
20+ private const string ProxyServer = "ProxyServer" ;
21+ private const string ProxyOverride = "ProxyOverride" ;
22+ private string _proxy = "" ;
23+ //private string bypass = "";
24+ private const string ByPassFile = "proxyByPass.ini" ;
25+
26+ public Form1 ( )
27+ {
28+ InitializeComponent ( ) ;
29+ }
30+
31+ private void Form1_Load ( object sender , EventArgs e )
1032 {
11- [ DllImport ( "wininet.dll" ) ]
12- public static extern bool InternetSetOption ( nint hInternet , int dwOption , nint lpBuffer , int dwBufferLength ) ;
13-
14- public const int InternetOptionSettingsChanged = 39 ;
15- public const int InternetOptionRefresh = 37 ;
16- private const string UserRoot = "HKEY_CURRENT_USER" ;
17- private const string Subkey = "Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Internet Settings" ;
18- private static string _keyName = "" ;
19- private const string ProxyEnable = "ProxyEnable" ;
20- private const string ProxyServer = "ProxyServer" ;
21- private const string ProxyOverride = "ProxyOverride" ;
22- private string _proxy = "" ;
23- //private string bypass = "";
24- private const string ByPassFile = "proxyByPass.ini" ;
25-
26- public Form1 ( )
33+ _keyName = UserRoot + "\\ " + Subkey ;
34+ var enbled = Registry . GetValue ( _keyName , ProxyEnable , 0 ) ;
35+ if ( enbled is int en )
2736 {
28- InitializeComponent ( ) ;
37+ chkEnabled . Checked = en == 1 ;
2938 }
3039
31- private void Form1_Load ( object sender , EventArgs e )
40+ var ps = Registry . GetValue ( _keyName , ProxyServer , "" ) ;
41+ if ( ps is string p )
3242 {
33- _keyName = UserRoot + "\\ " + Subkey ;
34- var enbled = Registry . GetValue ( _keyName , ProxyEnable , 0 ) ;
35- if ( enbled is int en )
36- {
37- chkEnabled . Checked = en == 1 ;
38- }
39-
40- var ps = Registry . GetValue ( _keyName , ProxyServer , "" ) ;
41- if ( ps is string p )
42- {
43- txtProxyServer . Text = p ;
44- _proxy = p ;
45- }
43+ txtProxyServer . Text = p ;
44+ _proxy = p ;
45+ }
4646
47- if ( File . Exists ( ByPassFile ) )
48- {
49- txtProxyByPass . Text = File . ReadAllText ( ByPassFile , Encoding . UTF8 ) ;
50- }
51- else
47+ if ( File . Exists ( ByPassFile ) )
48+ {
49+ txtProxyByPass . Text = File . ReadAllText ( ByPassFile , Encoding . UTF8 ) ;
50+ }
51+ else
52+ {
53+ var bs = Registry . GetValue ( _keyName , ProxyOverride , "" ) ;
54+ if ( bs is string b1 )
5255 {
53- var bs = Registry . GetValue ( _keyName , ProxyOverride , "" ) ;
54- if ( bs is string b1 )
55- {
56- var ax = b1 . Replace ( ";" , Environment . NewLine ) ;
57- txtProxyByPass . Text = ax ;
58- }
56+ var ax = b1 . Replace ( ";" , Environment . NewLine ) ;
57+ txtProxyByPass . Text = ax ;
5958 }
6059 }
60+ }
6161
6262
63- private void SetProxy ( )
63+ private void SetProxy ( )
64+ {
65+ if ( string . IsNullOrEmpty ( txtProxyServer . Text ) )
6466 {
65- if ( string . IsNullOrEmpty ( txtProxyServer . Text ) )
66- {
67- return ;
68- }
67+ return ;
68+ }
6969
70- _proxy = txtProxyServer . Text ;
71- if ( _proxy . Length != 0 )
72- Registry . SetValue ( _keyName , ProxyServer , _proxy ) ;
70+ _proxy = txtProxyServer . Text ;
71+ if ( _proxy . Length != 0 )
72+ Registry . SetValue ( _keyName , ProxyServer , _proxy ) ;
7373
74- Registry . SetValue ( _keyName , ProxyEnable , chkEnabled . Checked ? 1 : 0 , RegistryValueKind . DWord ) ;
75- File . WriteAllText ( ByPassFile , txtProxyByPass . Text ) ;
76- List < string > list = new List < string > ( Regex . Split ( txtProxyByPass . Text , Environment . NewLine ) ) ;
74+ Registry . SetValue ( _keyName , ProxyEnable , chkEnabled . Checked ? 1 : 0 , RegistryValueKind . DWord ) ;
75+ File . WriteAllText ( ByPassFile , txtProxyByPass . Text ) ;
76+ List < string > list = new List < string > ( Regex . Split ( txtProxyByPass . Text , Environment . NewLine ) ) ;
7777
78- var real = new List < string > ( ) ;
79- foreach ( string s in list )
78+ var real = new List < string > ( ) ;
79+ foreach ( string s in list )
80+ {
81+ if ( ! string . IsNullOrEmpty ( s ) && ! s . StartsWith ( "--" ) )
8082 {
81- if ( ! string . IsNullOrEmpty ( s ) && ! s . StartsWith ( "--" ) )
82- {
83- real . Add ( s ) ;
84- }
83+ real . Add ( s ) ;
8584 }
85+ }
8686
87- string bypass = string . Join ( ";" , real . ToArray ( ) ) ;
87+ string bypass = string . Join ( ";" , real . ToArray ( ) ) ;
8888
89- if ( bypass . Length != 0 )
90- Registry . SetValue ( _keyName , ProxyOverride , bypass ) ;
89+ if ( bypass . Length != 0 )
90+ Registry . SetValue ( _keyName , ProxyOverride , bypass ) ;
9191
92- // These lines implement the Interface in the beginning of program
93- // They cause the OS to refresh the settings, causing IP to realy update
94- InternetSetOption ( nint . Zero , InternetOptionSettingsChanged , nint . Zero , 0 ) ;
95- InternetSetOption ( nint . Zero , InternetOptionRefresh , nint . Zero , 0 ) ;
96- }
92+ // These lines implement the Interface in the beginning of program
93+ // They cause the OS to refresh the settings, causing IP to realy update
94+ InternetSetOption ( nint . Zero , InternetOptionSettingsChanged , nint . Zero , 0 ) ;
95+ InternetSetOption ( nint . Zero , InternetOptionRefresh , nint . Zero , 0 ) ;
96+ }
9797
98- private void btnSetProxy_Click ( object sender , EventArgs e )
99- {
100- SetProxy ( ) ;
101- }
98+ private void btnSetProxy_Click ( object sender , EventArgs e )
99+ {
100+ SetProxy ( ) ;
101+ }
102102
103- private void button1_Click ( object sender , EventArgs e )
103+ private void button1_Click ( object sender , EventArgs e )
104+ {
105+ ProcessStartInfo psi = new ( )
104106 {
105- ProcessStartInfo psi = new ( )
106- {
107- UseShellExecute = true ,
108- FileName = "ms-settings:network-proxy"
109- } ;
107+ UseShellExecute = true ,
108+ FileName = "ms-settings:network-proxy"
109+ } ;
110110
111- Process . Start ( psi ) ;
112- }
111+ Process . Start ( psi ) ;
113112 }
114113}
0 commit comments