I work in a school and i am currently working on a program which can switch the proxy settings on-the-fly in Windows Vista and Windows 7.
So far my code works fine if the browser is restarted but it doesn't on-the-fly. The settings will change once without a restart but then they wont take any effect at all. Would greatly appreciate any help at all. Here is the code from Main.cs (That matters anyway i've tried to keep it sweet)
Many Thanks in advance.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace ProxySwitch
{
public partial class Main : Form
{
[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void ProxyOnMenuItem_Click(object sender, EventArgs e)
{
try
{
if (File.Exists("Config.prsw"))
{
string ProxyAddress;
System.IO.StreamReader ConfigFile =
new System.IO.StreamReader("Config.prsw");
while ((ProxyAddress = ConfigFile.ReadLine()) != null)
{
RegistryKey InternetSettings = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
InternetSettings.SetValue("ProxyEnable", 1);
InternetSettings.SetValue("ProxyServer", (ProxyAddress));
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
ConfigFile.Close();
MessageBox.Show("The proxy settings have now been turned on!", "Successful!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("The configuration file could not be found! Please contact the system administrator", "Oh noes!",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch(System.Exception ErrorCode)
{
MessageBox.Show(ErrorCode.Message, "Oh noes!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ProxyOffMenuItem_Click(object sender, EventArgs e)
{
try
{
if (File.Exists("Config.prsw"))
{
string ProxyAddress;
System.IO.StreamReader ConfigFile =
new System.IO.StreamReader("Config.prsw");
while ((ProxyAddress = ConfigFile.ReadLine()) != null)
{
RegistryKey InternetSettings = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
InternetSettings.SetValue("ProxyEnable", 0);
InternetSettings.SetValue("ProxyServer", "");
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
ConfigFile.Close();
MessageBox.Show("The proxy settings have now been turned off!", "Successful!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("The configuration file could not be found! Please contact the system administrator", "Oh noes!",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (System.Exception ErrorCode)
{
MessageBox.Show(ErrorCode.Message, "Oh noes!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}