Skip to main content Skip to footer

How to Display Recent Files in the Taskbar of Your .NET Application

Have you ever wanted to customize the taskbar for your .NET application, such as opening a new instance of the application with a file loaded directly from your recent file list? The C1TaskbarButton control (part of ComponentOne WinForms Edition) allows you to do just that with only a few lines of code through its jump list features.

.NET Taskbar

In this blog, we’ll modify the taskbar button’s jump list to display all recently saved files for our C1WordPad application. Once a file is selected from the jump list, it will launch a new instance of the application with the newly-loaded file selected directly from the taskbar.

Ready to Test it Out? Download ComponentOne Today!

Getting Started

We’ll start with the C1WordPad sample and add the C1TaskbarButton control, which is part of the ComponentOne Windows Control Pack (C1.Win.Win7Pack package).

If you’d like to follow along with the steps included in this blog, please download the original, unmodified C1WordPad sample from our GitHub page here: ComponentOne-WinForms-Samples/NetFramework/Ribbon/CS/WordPad at master · GrapeCity/ComponentOne-WinForms-Samples · GitHub.

Add Files to the Taskbar Button Jump List

With only two lines of code, we can add files to our .NET application’s taskbar jump list:

// add to jumplist
c1TaskbarButton1.JumpList.Items.Add(new C1JumpPath() { Path = dlg.FileName, CustomCategory = "Recent Files" });
c1TaskbarButton1.JumpList.Apply();

Once you have the WordPad sample opened, let’s identify where we want to save and add files to the taskbar jump list.

The above code was added inside the SaveDocumentAs methods if statement right before the return on line 575 in the original WordPad sample application. The variable dlg.FileName is a string variable based on what the user just saved the file as. The fully modified SaveDocumentAs method (with only those two lines above added) can be seen below:

public bool SaveDocumentAs()
{
    using (SaveFileDialog dlg = new SaveFileDialog())
    {
        dlg.FileName = _documentName;
        dlg.Filter = "Rich text file (*.rtf)|*.rtf|"
            + "Rich text file, no OLE objects (*.rtf)|*.rtf|"
            + "Plain text file, no OLE objects (*.txt)|*.txt|"
            + "Plain text file, OLE objects replaced with text (*.txt)|*.txt|"
            + "Unicode text file, no OLE objects (*.txt)|*.txt";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            _documentName = dlg.FileName;
            _rdlSaved.AddDocument(_documentName);

            // add to jumplist
            c1TaskbarButton1.JumpList.Items.Add(new C1JumpPath() { Path = dlg.FileName, CustomCategory = "Recent Files" });
            c1TaskbarButton1.JumpList.Apply();
            return SaveDocument();
        }
    }
    return false;
}

The C1JumpPath object represents a file path you can add to the taskbar button jump list. There are other types of objects, such as C1JumpTask and C1JumpLink, so you can add additional functionality other than links to file paths. Read more about them here.

So far, we have added the files to the jump list, but it may not work just yet. To display recent files, your application must also be registered for the supported file types. We’ll cover that next.

Register Application for File Types

In order to display recent files in your .NET application’s taskbar, you’ll need to ensure the application is registered for the file types. Typically, this is done during the installation of your application to your users' computers. But for now, we can perform this functionality at runtime using the C1TaskBarButton library for ease of use.

For the C1WordPad application, we want to register our application to be associated with the .txt and .rtf file types. We can register file types at runtime using the C1TaskBarButton’s RegisterFileAssociations method.

For example, run the following code in the button click event:

private void button1_Click(object sender, EventArgs e)
{
    c1TaskbarButton1.WindowAppID = "WordPad";
    c1TaskbarButton1.RegisterFileAssociations(
        "Text Document",
        @"%systemroot%\system32\shell32.dll,284",
        string.Format("\"{0}\" \"%1\"", Application.ExecutablePath),
        new string[] { ".txt", ".rtf" });
}

Please note that you will have to run Visual Studio as an administrator for the default Microsoft security measures to not prevent you from properly registering the file types associated with your executable application. Setting the WindowAppID is also a necessary first step in order for the RegisterFileAssociations method to properly work. The value passed into the WindowAppID is the name of the application executable, which in this case is simply “WordPad”.

Configure the Application to Accept File Path Parameters

Now that we have added saved files to the recent jump list and our application can accept the types of files we will be passing into it, next, we need to also allow our application to accept and process the file. This is handled by an overloaded constructor argument which is the file that the jump list passes when we select our recent file from the list above the taskbar icon. We can configure our application to accept this by changing the Main entry point within our Program.cs file to look like this:

static void Main (string[] args)
{
    Application.EnableVisualStyles ();
    Application.SetCompatibleTextRenderingDefault (false);
    if (args.Length > 0)
        Application.Run(new Form1(args[0]));
    else
        Application.Run (new Form1());
}

The if/else statements above act as the trigger to accept the newly passed-in argument once we decide to select a file from the jump list. When we first initially run the project, no additional argument is passed in. Thus the first if statement fails, and the else statement will start a new form without an argument passed.

Next, we’ll have to configure our form’s constructor class to accept an optional parameter, which in C# is completed by creating a variable inside the Form1 parameters that is set to the default value for its variable type. In this case, we are passing in a string, and the default value for a string variable is an empty string represented by an empty set of double quotation marks, just like the line below:

public Form1(string filename = "")

Within Form1, we’ll also need to set an if/else statement to ensure that we are loading in a starting value (if you don’t want to launch the application with an initial blank WordPad) and can check if we are passing in an argument to the Form1 constructor, which is the file passed from the C1JumpList. That if/else statement will replace the standard load on line 33 with the following:

// Load a sample text file into the editor.
if (filename == "")
    this.LoadDocument(@"Readme.rtf");
else
    this.LoadDocument(filename);

Now, we can launch our application, click on our button to register our file types, and inside our Taskbar Icon’s jump list, all of our recent files will be shown (Note, in this example, these files are being taken from the project’s bin/debug/ folder by default). If we select one of these files, a new instance of the WordPad application will be launched with the selected file already loaded in, thanks to the registration and constructor overload code we added earlier that would accept an argument passed in from the jump list when calling an instance of the application.

.NET Taskbar

And that’s it! If you’re interested, here is the fully completed sample.

If this is your first time hearing about ComponentOne controls, you can explore the full suite’s capabilities and functionality by downloading our free 30-day trial directly on our website. Download Our Powerful .NET UI Controls | ComponentOne

Happy coding!

 

comments powered by Disqus