本文讲解了在C#程序中设置代理和取消代理的方法,原理很简单,就是通过调用C#操作注册表的相关类来对注册表的相关内容进行修改而实现的,具体源代码如下:
1、启动代理
private void button3_Click(object sender, EventArgs e){//打开注册表键 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); //设置代理可用 rk.SetValue("ProxyEnable", 1); //设置代理IP和端口 rk.SetValue("ProxyServer", this.textBox1.Text.ToString() + ":" + this.textBox2.Text.ToString()); rk.Close(); Factory.ExecuteNonQuery("update IP set area='1' where address= '" + str1 + "'"); this.dataGridView1.DataSource = Factory.GetDataTable("select * from ip"); MessageBox.Show("设置成功!!!");}
2、停止代理
private void button4_Click(object sender, EventArgs e){ //打开注册表键 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); //设置代理不可用 rk.SetValue("ProxyEnable", 0); rk.Close(); MessageBox.Show("设置成功!!!");}
本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!