本文講解了在C#程序中設(shè)置代理和取消代理的方法,原理很簡單,就是通過調(diào)用C#操作注冊表的相關(guān)類來對注冊表的相關(guān)內(nèi)容進(jìn)行修改而實(shí)現(xiàn)的,具體源代碼如下:
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);
//設(shè)置代理可用
rk.SetValue("ProxyEnable", 1);
//設(shè)置代理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("設(shè)置成功!!!");
}
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);
//設(shè)置代理不可用
rk.SetValue("ProxyEnable", 0);
rk.Close();
MessageBox.Show("設(shè)置成功!!!");
}
新聞熱點(diǎn)
疑難解答
圖片精選