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.
reg add "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings" /t REG_DWORD /v ProxyEnable /d %inet% /f
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
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
Now we want to let our users know what this silverlight application can do so we’ll provide them with some instructions
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.
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
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.
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;
RegistryKey RegKeyRead = Registry.CurrentUser;
if ((string)regSuccessful == "TRUE")
If you press F5 now you will be able to turn your proxy server on and off with a simple application
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