Click here to Skip to main content
15,353,416 members
Articles / Desktop Programming / XAML
Posted 16 Jun 2010


48 bookmarked

Silverlight 4: Accessing Windows 7 Taskbar using the new COM API

Rate me:
Please Sign up or sign in to vote.
4.91/5 (27 votes)
17 Jun 2010CPOL10 min read
Silverlight 4 has the power to talk with any other application using the COM API. After reading this article, you will be able to pin/unpin any Windows application to your Windows 7 taskbar directly from your Silverlight 4 out-of-browser application.

Table of Contents


Microsoft released Windows 7 last year which has lots of functionalities including a nice UI. Among those features, one of the most user friendly is Pin/Unpin application to Taskbar. You can develop WPF or Windows Forms applications in which you can implement this feature. But do you know that this is also possible in Silverlight? Yes, Silverlight 4 has the power to talk with any other application using the COM API. You can read one of my earlier articles [Silverlight 4: Interoperability with Excel using the COM Object].

So, how can I develop such functionality in my Silverlight 4 application? Here I will describe it in a step-by-step manner to let you know about the feature. After reading this article, you will be able to pin/unpin any Windows application to your Windows 7 taskbar directly from your Silverlight 4 out-of-browser application.


In Windows 7 operating system, you can pin any application for easy access like the earlier quick launch toolbar in a new way. Once you right click on the taskbar icon, you will get some quick tasks right to the context menu as you do for regular day-to-day activity. There is an option to Pin/Unpin the application in taskbar. Once pinned, you can open that application directly from the taskbar. Have a look at the below snapshot:


The pinned applications which are not opened will look like a flat icon whereas the opened applications (whether they are pinned or not) will have a rectangular border with some glossy background as shown above.

When you right click on an unpinned application from the taskbar, you will see a Menu having an option to pin that program to the taskbar like this:


Once pinned to the taskbar, if you again right click on the icon, you will see that the “Pin this program to taskbar” menu item has been changed to “Unpin this program from taskbar”. Have a look at the below figure:


Silverlight 4 has a great feature like accessing the Taskbar using the COM API. In my earlier article, I demonstrated another feature called “Accessing Microsoft Excel Application using Silverlight 4 COM API”. In this article, I will demonstrate how you can access the taskbar. Read the full article and provide your feedback/suggestions.


To develop this simple application, you need the following tools installed in your development environment:

  • Windows 7 Operating System (required to run the application)
  • Visual Studio 2010
  • Silverlight 4 Tools for Visual Studio 2010

Remember that Silverlight 4 applications can be developed only in Visual Studio 2010. Hence, if you have Visual Studio 2008 installed in your PC, you can install Visual Studio 2010 side-by-side for exploring Silverlight 4.

Getting Started

If your development environment is ready, then we can proceed towards creating a new Silverlight Application project. At the end of this part, we will be able to run our first Silverlight application inside the browser.

  1. Open your Visual Studio 2010 IDE
  2. Select File > New Project or just press CTRL + SHIFT + N to open up the New Project dialog
  3. Expand the “Visual C#” node and then go to sub node “Silverlight”
  4. Select “Silverlight Application” in the right pane
  5. Select proper location to store your application (let’s say, “D:\Sample Apps\
  6. Now enter a proper name for your project (call it as: “Silverlight4.Interoperability.Taskbar”)
  7. Select the .NET Framework version from the combo box at the top (I am using .NET Framework 4.0)
  8. Click OK to continue
  9. In the next dialog, make sure that “Host the Silverlight application in a new Web site” option is selected
  10. Choose “Silverlight 4” as the Silverlight Version and hit OK

Wait for a while, Visual Studio will now create the Silverlight solution for you to use which will contain a Silverlight Project and one Web Application Project to host your Silverlight application. In your Silverlight project, you will find a “MainPage.xaml” & an “App.xaml” file which are already created for you by the IDE Template.

Once you are done with setting up the project, you need to add an Assembly Reference to the Silverlight project. We will use the “dynamic” keyword and for this we need to add the “Microsoft.CSharp” assembly reference.

  1. Right click on the “Reference” folder inside the Silverlight project and click on the “Add Reference” menu item from the context menu.


  2. This will open up the “Add Reference” dialog on the screen. Scroll the window to find the assembly named “Microsoft.CSharp” from the .NET tab. Select it and click “OK”. This will add the Microsoft.CSharp assembly reference into your Silverlight project. Once added, your project will support dynamic variable declaration.


Configuring Out-Of-Browser Settings

Once you are done with setting up your Silverlight project, we are ready to implement custom Out-of-Browser Window for our application. You can read the complete article on Creating Silverlight 4 Custom Out-of-Browser window from CodeProject.

Once you design your out-of-browser Window, go to the properties of the Silverlight project. From the Silverlight pane, be sure that you are using “Silverlight 4” as target version. Now select the “Enable running application out of browser” which will make the “Out-of-Browser Settings…” button enabled. Click on it for more settings.


From the Settings dialog window, select “Show install menu” which will create a Menu Item inside the Silverlight context menu. Once you run your application and right click on the application, you will see an “Install” menu item on it. I will come to this section later.


Now, check the “Require elevated trust when running outside the browser” as shown above and choose “Window Style” as “No Border”. Once you are done with these settings, click ok to save the configurations. You can also change the “Window Title”, “Size” and other options available there.

Designing the XAML Page

Let us start designing the XAML page for showcasing our example. In our demo example, we will add three TextBox controls along with three Buttons. When the application starts, it will populate the location of the files (e.g. Wordpad, Paint and Calculator) which we want to Pin/Unpin into the Windows 7 Taskbar inside the TextBoxes. There will be a button near to each Textbox. Once clicked on the button, it will Pin the application in the Taskbar (provided the application is not already pinned). If the application is already pinned to the taskbar, it will Unpin from there.

Below is the complete XAML code of my example:

<UserControl x:Class="Silverlight4.Interoperability.Taskbar.MainPage"
    <Border BorderBrush="Black"
        <Grid x:Name="LayoutRoot"
                <RowDefinition Height="31" />
                <RowDefinition />

            <Border BorderBrush="Black"
                    <LinearGradientBrush EndPoint="0.5,1"
                        <GradientStop Color="Black" />
                        <GradientStop Color="Black"
                                      Offset="1" />
            <Border BorderBrush="Black"
                    <LinearGradientBrush EndPoint="0.5,1"
                        <GradientStop Color="White" />
                        <GradientStop Color="Transparent"
                                      Offset="0.69" />
                <Border x:Name="brdClose"
                    <TextBlock TextWrapping="Wrap"
                               IsHitTestVisible="False" />
            <Border BorderBrush="Black"
                    Grid.Row="1" />
            <TextBlock HorizontalAlignment="Left"
                       Text="Windows 7 Taskbar Demo"
                       Foreground="White" />
            <TextBox x:Name="txtWordpad"
                     Background="White" />
            <Button x:Name="btnWordpad"
                    d:LayoutOverrides="Width, Height"
                    Foreground="White" />
            <TextBox x:Name="txtPaint"
                     Background="White" />
            <Button x:Name="btnPaint"
                    d:LayoutOverrides="Width, Height"
                    Foreground="White" />
            <TextBox x:Name="txtCalculator"
                     Background="White" />
            <Button x:Name="btnCalculator"
                    d:LayoutOverrides="Width" />
            <TextBlock x:Name="txbMessage"
                       FontSize="12" />

Playing with the Code

Let us have some fun writing the code. First of all, we will populate our TextBoxes with the location of three different applications (Wordpad, Paint and Calculator). Remember that there are some restrictions in Silverlight while accessing local files. Hence we will use some trusted location for our case.

The method GetFolderPath() will return the Start Menu -> All Programs -> Accessories directory which we will set as the value of the TextBox after appending the Filename. Before setting the path, let us discuss about the lines inside the method.

  1. As a first step, we need to create the Shell Application object from the AutomationFactory.
  2. Once we have the Shell Application instance, we can get the common programs folder. We can get it by passing the value "23" to the NameSpace method. 23 is the namespace of the Windows Common Programs directory path. We will store it in a local dynamic variable named "commonPrograms" for later use.
  3. commonPrograms.Self.Path will return the exact path of the Common Programs folder.
  4. We will append "Accessories" with this path so that we can get the exact location from where we have to pickup the Application files.
private string GetFolderPath()
    using (dynamic shellApplication = AutomationFactory.CreateObject("Shell.Application"))
        // get the common programs folder using the namespace
        dynamic commonPrograms = shellApplication.NameSpace(23);

        // commonPrograms.Self.Path will return =>	
        // "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs"
        string folderPath = string.Format("{0}\\{1}",
            commonPrograms.Self.Path, "Accessories");

        return folderPath;

On page load, we will call this method and populate the TextBox field with the exact location. Here is the code for it:

void MainPage_Loaded(object sender, RoutedEventArgs e)
    txtWordpad.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Wordpad");
    txtPaint.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Paint");
    txtCalculator.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Calculator");

If you run the application now, it will look as below:


Let us now go more into depth to implement a method which will take the filename as a parameter and check if the program is already pinned to the taskbar or not. If it is not pinned, the code will pin the application and if it is already pinned, it will unpin that. Let's write the code:

private void PinToTaskbar(string fileName)
    // create the shell application object
    using (dynamic shellApplication = AutomationFactory.CreateObject("Shell.Application"))
        // get the accessories directory
        dynamic directory = shellApplication.NameSpace(GetFolderPath());

        // get the full application path
        dynamic link = directory.ParseName(fileName + ".lnk");

        dynamic verbs = link.Verbs();
        for (int i = 0; i < verbs.Count(); i++)
            dynamic verb = verbs.Item(i);
            string verbName = verb.Name.Replace(@"&", string.Empty).ToLower();

            if (verbName.Equals("pin to taskbar"))
                ShowMessage(fileName + " has been pinned to your taskbar.");
            else if (verbName.Equals("unpin from taskbar"))
                ShowMessage(fileName + " has been unpinned from your taskbar.");

The first line is the same as described earlier. Once we have the Shell Application instance, we will get the Accessories directory from the shell namespace by calling the GetFolderPath(). Now append the filename with the extension to the whole path. We have to use .lnk as extension here because we are accessing it from the Start Menu folder where all the applications have a shortcut link which is nothing but an extension of .lnk.

Once we have the full application path, we can retrieve the context menu items assigned to the application by calling Verbs(). Now, we will iterate through each item and retrieve the item name:

dynamic verb = verbs.Item(i);
string verbName = verb.Name.Replace(@"&", string.Empty).ToLower();

Now we will check if the item name is "pin to taskbar", means the application is not yet pinned to your taskbar. So pin it to taskbar by calling verb.DoIt(). If the item name is "unpin from taskbar", means it is already pinned to the taskbar. Hence, we can unpin it by calling the verb.DoIt() again. The call to verb.DoIt() actually executes the shell to run the selected item command.

On the button click implementation, we have to just call this method by passing the application name, i.e., "Wordpad", "Paint", "Calculator" etc. and once clicked it will check the name and do what we exactly told it to do.

Playing with the Application

It was a big time writing the code & designing the UI for our application. Now let us run the application and see what we have implemented till now.

Once it runs your application (if it opened in Browser mode, please install it as Silverlight Out-of-Browser application and run from your desktop), you will see the following Window:


Click the "Pin/Unpin" button near to the TextBox for "Wordpad" application. You will see that the Wordpad icon has been added to your Windows 7 Taskbar (provided that the icon is not already pinned) and it prompts a message "Wordpad has been pinned to your taskbar." Let us do the same for the other buttons also. You will see that the applications are pinning to the taskbar when you clicked on the respective button.



Let us click the buttons again one by one. You will notice that the applications are unpinned one by one from the taskbar as and when you are clicking the respective button and prompts a message "<APP_NAME> has been unpinned from your taskbar."



Wao, what a nice feature!!! Yeah, that's great. We can access the Windows 7 taskbar directly from the Silverlight application (OOB only). Earlier only Windows standalone applications had the rights to access it. Now it's the time of Silverlight too.


  • Initial post - 18th June, 2010

End Note

I think, from this article you got the initial idea on the Silverlight COM API to access the Windows 7 Taskbar. Now you can think about your business application to do something new in Silverlight. Many more functionalities are there. Not only this, you can even access the Windows Registry too. So, explore more using the COM API to fulfill your business need.

If you are interested, you can download the sample source code written for this article. Not only this, you can even download the demo zip. Just run the file named "install". It will install the Silverlight XAP application automatically and create the shortcut to the application in your Desktop and Startmenu. Open the file to execute the sample application.

Please vote for this article and don't forget to provide your feedback/suggestions. This will give me an opportunity to improve my articles.

Enjoy reading my articles @CodeProject and @MyBlog


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog ( and CodeProject.

Books authored:

Connect with Kunal on:

Comments and Discussions

GeneralMy vote of 1 Pin
UOM21-Jul-10 7:06
MemberUOM21-Jul-10 7:06 
AnswerRe: My vote of 1 Pin
Kunal Chowdhury «IN»21-Jul-10 15:52
professionalKunal Chowdhury «IN»21-Jul-10 15:52 
GeneralRe: My vote of 1 Pin
UOM22-Jul-10 1:00
MemberUOM22-Jul-10 1:00 
GeneralRe: My vote of 1 Pin
Kunal Chowdhury «IN»22-Jul-10 7:06
professionalKunal Chowdhury «IN»22-Jul-10 7:06 
GeneralMy vote of 3 Pin
amolbodake3-Jul-10 8:33
Memberamolbodake3-Jul-10 8:33 
GeneralRe: My vote of 3 Pin
Kunal Chowdhury «IN»4-Jul-10 8:34
professionalKunal Chowdhury «IN»4-Jul-10 8:34 
GeneralNice Pin
Shivprasad koirala27-Jun-10 17:06
MemberShivprasad koirala27-Jun-10 17:06 
GeneralRe: Nice Pin
Kunal Chowdhury «IN»27-Jun-10 18:10
professionalKunal Chowdhury «IN»27-Jun-10 18:10 
GeneralThanks Pin
Sachin Gorade21-Jun-10 19:28
MemberSachin Gorade21-Jun-10 19:28 
GeneralRe: Thanks Pin
Kunal Chowdhury «IN»21-Jun-10 21:26
professionalKunal Chowdhury «IN»21-Jun-10 21:26 
GeneralGood stuff - Well explained with lots of Pictures!!! Pin
Alan Beasley20-Jun-10 0:14
MemberAlan Beasley20-Jun-10 0:14 
GeneralRe: Good stuff - Well explained with lots of Pictures!!! Pin
Kunal Chowdhury «IN»20-Jun-10 0:20
professionalKunal Chowdhury «IN»20-Jun-10 0:20 
GeneralGood stuff Pin
Abhinav S19-Jun-10 7:27
MemberAbhinav S19-Jun-10 7:27 
GeneralRe: Good stuff Pin
Kunal Chowdhury «IN»19-Jun-10 7:31
professionalKunal Chowdhury «IN»19-Jun-10 7:31 
GeneralGood Step by Step Pin
Habib El-Zoghbi18-Jun-10 8:30
MemberHabib El-Zoghbi18-Jun-10 8:30 
GeneralRe: Good Step by Step Pin
Kunal Chowdhury «IN»18-Jun-10 9:38
professionalKunal Chowdhury «IN»18-Jun-10 9:38 
QuestionDownloads? Pin
Chris Maunder18-Jun-10 2:05
cofounderChris Maunder18-Jun-10 2:05 
AnswerRe: Downloads? Pin
Kunal Chowdhury «IN»18-Jun-10 10:11
professionalKunal Chowdhury «IN»18-Jun-10 10:11 
GeneralGreat Man ! Pin
Abhijit Jana17-Jun-10 19:47
professionalAbhijit Jana17-Jun-10 19:47 
GeneralRe: Great Man ! Pin
Kunal Chowdhury «IN»17-Jun-10 20:11
professionalKunal Chowdhury «IN»17-Jun-10 20:11 
GeneralLove the layout [modified] Pin
defwebserver17-Jun-10 16:36
Memberdefwebserver17-Jun-10 16:36 
GeneralRe: Love the layout Pin
Kunal Chowdhury «IN»17-Jun-10 20:10
professionalKunal Chowdhury «IN»17-Jun-10 20:10 
GeneralAmaizing work Pin
Abhishek Sur17-Jun-10 9:29
professionalAbhishek Sur17-Jun-10 9:29 
GeneralRe: Amaizing work Pin
Kunal Chowdhury «IN»17-Jun-10 20:09
professionalKunal Chowdhury «IN»17-Jun-10 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.