in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Alex Pearce's SharePoint Blog

Alex Pearce

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

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_USER\Software\Microsoft\Windows\CurrentVersion\Internet 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

 

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

<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 button 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;
           RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
           RegKeyWrite.SetValue("ProxyEnableScript", "TRUE");
           RegKeyWrite.SetValue("ProxyEnable", 1);
           RegKeyWrite.Close();

           RegistryKey RegKeyRead = Registry.CurrentUser;
           RegKeyRead = RegKeyRead.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet 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

 

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: ,
Published May 06 2008, 07:18 PM by apearce
Filed under:

Comments

 

My first foray with Silverlight.. - EduGeek.net Forums said:

Pingback from  My first foray with Silverlight.. - EduGeek.net Forums

May 29, 2008 2:33 AM
 

instructions for using microsoft access said:

Pingback from  instructions for using microsoft access

June 1, 2008 7:10 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About apearce

My name is Alex Pearce and I live in Birmingham, UK. I am the Learning Environment and ICT Manager at the largest school in the UK. I am implementing the Microsoft Learning Gateway into my 12th school and planning on hosting 60 primary (elementary) school in the same environment. SharePoint is the portal layer to the Microsoft Learning Gateway and I first started using SPS2003 before moving onto MOSS 2007 when it was released in late 2006. I am hoping to share my knowledge with you all. Why I hear you ask!!?! I work for an education organization and the reason I enjoy my job so much is because I want to help pupils/students achieve what they want in life. Why should I hide my secrets? Why not help other schools implement the Microsoft Learning Gateway/SharePoint solution to improve learning in their school. I hope you find my blog useful . www.sharepointblogs.com/ajp

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts