Silverlight 2 Application to enable/disable your proxy server in IE

Standard

I’ve been playing around a lot with Silverlight 2 recently, I’m find it a lot easier to code than version 1.

In many of the schools I have been to, they have had a proxy server for all their internet traffic.  I created a bat script that wrote to the registry and asked the user to either type 1 to turn the proxy on or 0 for off.  It worked great but wasn’t easy on the eye and one network I went to it stopped the users from using command lines.

@echo off

Echo.
echo.
Echo                     Please type the following
echo.
echo —————————————————————-
echo —————————————————————-
Echo —      Proxy server must be enabled to work in school      —
echo — Type ‘1’ if you wish to enable internet access in school —
echo —————————————————————-
Echo —       Proxy server must be disabled to work at home      —
Echo —     Type ‘0’ If you wish to disable the proxy server     —
echo —————————————————————-
echo —————————————————————-
Echo.
Echo.
Set /p inet=   Type ‘Number 1’ or ‘Number 0’ here:

reg add "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /t REG_DWORD /v ProxyEnable /d %inet% /f
pause

So I thought it was time to create something that was more graphical.  It would also solve the issue of a user typing 2 on the above script and causing errors in the registry.

So this is what we are going to create

 Proxy1

Download the Microsoft Expression Blend 2.5 March Preview (available at time of writing this post).

Create a WPF Application and call it ProxyEnableDisable

Add 2 buttons and name one Proxy On and the other Proxy Off

 proxy2

Now we want to let our users know what this silverlight application can do so we’ll provide them with some instructions

 Proxy3

We’ve done everything we need to now in Expression Blend so we need to move the project over to Visual Studio.  Make sure you save your project.

Over on the write hand side you will see Solution ‘ProxyEnableDisable’. Right click here and click on Edit in Visual Studio.

 proxy4

This should have now opened up Visual Studio.  You will see the same layout above, down the right hand side.  Double click on Windows1.xaml.  This is the silverlight coding.  If your using Visual Studio 2008 you will see the xaml code and a graphical view of our project.

We need to start adding come code to the buttons.  We’ll give it a x:name and then add a click handler for both of the buttons.

Find the line of code for the button with the content that is equal to Proxy On and add x:name="On".  Also type click and click on <NewEventHandler>.  This will allow us to add come C# handling code to this button.

Your line of code should now read

<Button HorizontalAlignment="Left" Margin="105,201,0,193" Width="150" Content="Proxy On" x:Name="On" Click="On_Click" />

Do the same for the Proxy Off button but instead of giving the x:name the value of On – call it off

This is the last bit of XAML coding for this project.  The window1.xaml should now read

<Window
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Proxy_Enable.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <Button HorizontalAlignment="Left" Margin="105,201,0,193" Width="150" Content="Proxy On" x:Name="On" Click="On_Click" />
        <Button HorizontalAlignment="Right" Margin="0,201,145,193" Width="150" Content="Proxy Off" x:Name="Off" Click="Off_Click" />
        <TextBox Margin="105,78,145,0" VerticalAlignment="Top" Height="101" Text="For you to use your laptop away from school you require the proxy server to be turned off.  Click on Proxy Off.&#xd;&#xa;&#xd;&#xa;For you to use your laptop at school you require the proxy server to be turned on.  Click on Proxy On." TextWrapping="Wrap"/>
    </Grid>
</Window>

Expand the Windows1.xaml and click on windows1.xaml.cs

We’re now going to add some C# coding that will write to our registry to either add turn the proxy on or off depending on which bu
tton the user clicks on.

 prox5

We need to add a reference to our C# code.  At the top add Using Microsoft.Win32;

The below code will turn the proxy server on.  The 4th line down is where you are telling the registry to either have the proxy server enable or disabled.  The value of 1 will turn the proxy on where 0 will disable it.  Add the line of code to both of your button event handlers remembering to change that 1 to 0 for proxy off.

RegistryKey RegKeyWrite = Registry.CurrentUser;
           RegKeyWrite = RegKeyWrite.CreateSubKey("SoftwareMicrosoftWindowsCurrentVersionInternet Settings");
           RegKeyWrite.SetValue("ProxyEnableScript", "TRUE");
           RegKeyWrite.SetValue("ProxyEnable", 1);
           RegKeyWrite.Close();

           RegistryKey RegKeyRead = Registry.CurrentUser;
           RegKeyRead = RegKeyRead.OpenSubKey("SoftwareMicrosoftWindowsCurrentVersionInternet Settings");
           Object regSuccessful = RegKeyRead.GetValue("ProxyEnableScript");
           Object regAttemptNumber = RegKeyRead.GetValue("ProxyEnable");
           RegKeyRead.Close();

           if ((string)regSuccessful == "TRUE")
               Console.WriteLine("Succeeded on attempt # {0}", regAttemptNumber);

If you press F5 now you will be able to turn your proxy server on and off with a simple application

 proxy6

Now its all yours to be customised.

Don’t forget to install Silverlight 2 on your clients before rolling it out.

Click here to download the bat file

Click here to download the application

Click here to download the source code

Technorati Tags: ,

2 thoughts on “Silverlight 2 Application to enable/disable your proxy server in IE

Leave a Reply

Your email address will not be published. Required fields are marked *