Over a million developers have joined DZone.

Using Windows 7 libraries in .NET


With the release of Windows 7, a new feature was introduced – libraries. A library is basically a content placeholder for any files or folders. Being a virtual entity, a user doesn’t copy the folder or file to a library, but much rather adds a reference to it. This gives the possibility to make a file or folder a member of multiple libraries.

For example, a user has a file called reports.txt and two libraries – Reports and ToSend. Later on, he adds the reference to reports.txt on both Reports and SendTo. The file remains in its original location, while both libraries contain the reference. This makes file organization and tracking a lot easier.

To use Windows 7 in .NET, a developer has the option to use the Windows API Code Pack for .NET Framework, which facilitates access to this functionality by providing a WinAPI wrapper for managed environments.

Once the Windows Code Pack is downloaded, there are two options available for its usage – the developer can either import the Windows API Code Pack projects in an existing solution an reference those to use the functionality or separately compile the projects in standalone DLLs and then use them as component parts of a software solution.

The second option is more reliable, therefore I compiled two projects needed for the Windows 7 libraries functionality to be available – Core (located in the Core subfolder once extracted) and Shell (located in the Shell subfolder once extracted). Then, in my Windows Forms application I added two references – to Microsoft.WindowsAPICodePack.dll and to Microsoft.WindowsAPICodePack.Shell.dll.

I also added the proper namespace references:

using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Shell;

Now I am going to show how to create a Windows 7 library. I created a form, that has a Button control on it. In the Click event handler for the button I added the following code:

ShellLibrary library = new ShellLibrary("Test Library", false);

Once the Click event is triggered, a library named Test Library will be created. The false parameter determines whether the new library will overwrite the existing library, in case one is found that has the same name. The newly created library will appear in Windows Explorer:

Another cool feature to play with when using Windows 7 libraries is the possibility to set custom icons for them. This is done by using code similar to this:

library.IconResourceId = new IconReference(Assembly.GetExecutingAssembly().Location, 0);

Important Note: A reference to System.Reflection is needed to use the Assembly class.

Since I have the default icon changed for the executable, the code above will get the icon with index 0 from the resource file bound to the executable, therefore getting the result below:

Pretty neat so far. A developer can also add and remove folders to the newly created library. To add a folder, I used this code:


Important Note: Using the code above you cannot add separate files. The parameter that is passed must be a folder path.
To remove a folder, I used this:


Not always the file is added and removed at the time the library was created. How would it be possible to reference an existing library? Quite simple, actually. All I had to do is use this:

string libPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ShellLibrary.LibrariesKnownFolder.RelativePath);

ShellLibrary library = ShellLibrary.Load("Test Library", libPath, false);

Notice that library structure data is kept in the AppData folder on a Windows 7 machine, separate for each registered user on the machine.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}