Setting IE proxy from vb.net applications


Changing Internet Explorer Proxy settings from .net application.

I have made a class which you can use in your applications and just call the Methods to Set Proxy or Disable Proxy for your Internet Explorer Browser from your DotNet Application.


Imports Microsoft.Win32
Public Class ProxySetting
Public Function IsProxyEnabled() As Boolean
Try
Dim Regs As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.ReadWriteSubTree)
If Regs.GetValue("ProxyEnable") <> Nothing Then
If Regs.GetValue("ProxyEnable").ToString() = "0" Then
Return False
Else
Return True
End If
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function

Public Function GetProxyServer() As String
Try
Dim Regs As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", RegistryKeyPermissionCheck.ReadWriteSubTree)
If Regs.GetValue("ProxyServer") <> Nothing Then
Return Regs.GetValue("ProxyServer").ToString()
Else
Return ""
End If
Catch ex As Exception
Return ""
End Try
End Function

Public Sub DisableProxy()
Dim regKey As RegistryKey
Try
regKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
regKey.SetValue("ProxyEnable", False, RegistryValueKind.DWord)
regKey.Close()
Catch ex As Exception

End Try
End Sub

Public Sub SetProxy(ByVal ServerName As String, ByVal port As Integer)
Try
Dim regkey1 As RegistryKey
regkey1 = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
regkey1.SetValue("ProxyServer", ServerName + ":" + port.ToString(), RegistryValueKind.Unknown)
regkey1.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
regkey1.Close()
Catch ex As Exception

End Try
End Sub

End Class

How to use the class and its methods
1) To Check Whether Proxy Is Enabled or Disabled call the Function IsProxyEnabled()
Example :

Dim Proxy as new ProxySetting()
If pxy.IsProxyEnabled() Then
Msgbox("Proxy Enabled")
End If

2) Set IE Proxy you need to call the SetProxy() method which takes 2 Parameters the first the IP or the Host Name and the second the Port Number.
Example

Proxy.SetProxy(TxtProxy.Text, Convert.ToInt16(txtPort.Text))


3) Disable Proxy Settings you need to call the Method DisableProxy()

Proxy.DisableProxy()

4) To get what proxy is already applied you need to call the Function GetProxyServer();

Dim ServerAndProxy() As String = Proxy.GetProxyServer().Split(":")
TxtProxy.Text = ServerAndProxy(0)
txtPort.Text = ServerAndProxy(1)

In the code above i split the Result of GetProxyServer() with the seperator : to get the Host and the IP seperately.

Regards
Hefin Dsouza


Comments

Guest Author: Mudasir Mirza11 Feb 2012

This code is not working. I tried it, not getting any errors, but proxy is not being set in IE.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: