We are wrapping up our comprehensive drilldown into the Windows 7 Libraries API and architecture with one final post about the managed API for working with Windows 7 Libraries. Previously we talked about: Understanding Windows 7 Libraries, Libraries Under the Hood, Light Up with Windows 7 Libraries, Consuming the Contents of Windows 7 Libraries, Stay in Sync with Windows 7 Libraries, Windows 7 Libraries Helpers
In this post we focus on the managed API for working with Windows 7 Libraries. The Windows API Code Pack for the Microsoft .NET Framework Library (Windows API Code Pack) provides a .NET interface into the Windows native API, thereby enabling the 7 key Windows Light-Up features as well as tapping into more advanced shell integration, as we described in the Windows 7 Managed Code APIs post. You can download the Windows Code Pack API
ShellLibrary library = new ShellLibrary(“new test lib1”, true);
ShellLibrary library = ShellLibrary.Load("My Pictures", true);
public void Add(FileSystemFolder item);public void Add(string folderPath);public bool Remove(FileSystemFolder item);public bool Remove(string folderPath);
String dsfStr = library.DefaultSaveFolder
Note that when setting the default save folder, you need to provide a valid system folder for which you have write and read permission. If this folder is not already part of the library, it will be added to the library and will become the default save location. As a developer, you can also add unsupported folder locations that are not indexed and can’t be indexed.
Another very cool feature is control of the library icon. This one of those features that end users don’t have access to, and it is SO easy to use, as shown in the following code snippet:
library.IconResourceId = new IconReference(icon);
You will have to obtain an IconReference object, but after doing so you get to change the library’s Icon and set your own special icons. This is a really fun feature to play with, as you can see from the following picture:
Another important feature of the ShellLibrary object is the capability to list all the items and folders in a library. The ShellLibrary object implements the IList<FileSystemFolder> interface. This supports enumeration of the folders in the library by calling the GetEnumerator function or by using the foreach statement as shown in the following code snippet that prints the folder’s name and default save location:
foreach (ShellFolder folder in library){ Console.WriteLine( "\t\t{0} {1}", folder.Name, defaultSaveFolder == folder.Name ? "*" : "")}
You can control the library type by using the LibraryType property, and you can elect to pin the library listing to the left navigation pane of Windows Explorer using the IsPinnedToNavigationPane property.
Last, I want to talk about displaying the Library Manager window. It is important to display this window if you wish to create a consistent user experience for managing libraries in Windows Explorer and your application. Since displaying the Library Manager window displays a new dialog window in an existing application, you need to take into account the technology used by the application. The function ShowManageLibraryUI can receive either a Win32 window handle (whnd) that will work great with WinForms, or a WPF windows object for WPF applications.
library.ShowManageLibraryUI(IntPtr.Zero, “title of dialog”, "help text…", true);
public static void DeleteLibrary(string name){ string librariesPath = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), ShellLibrary.LibrariesKnownFolder.RelativePath); string libraryPath = Path.Combine(librariesPath, name); string libraryFullPath = Path.ChangeExtension(libraryPath, "library-ms"); File.Delete(libraryFullPath);}
We have covered all the important functionality of the Libraries managed code API and, together with the previous posts; you now should have a very good sense of how Windows 7 Libraries work and how to use them in your applications.
In addition, you can get first-hand coding experience with Windows 7 by going through the Windows 7 Libraries Hands-On-Lab (HOL) that is part of the Windows 7 RC Training Kit for Developers. The Library HOL includes the code used in this post, so go ahead and download the training kit.