Windows 7 Libraries C# Quick Reference

March 15, 2010

Following are some listings to be used as a quick reference to common Windows 7 Libraries features using Windows API Code Pack.


The code in this post is based on previous work by Alon and other Sela team members. Great work all.


Forward


Just to clarify the terminology used:
Every Windows 7 library is represented as an XML file with the .library-ms extension.
The common libraries files are commonly stored in: C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\


So if, for example, we work now with the Pictures library, than in the following sections:
libraryName = Pictures
locationPath = C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\


Note: you can create library files wherever you want, not necessarily in the mentioned folder.


Features


Creating a new library




ShellLibrary shellLibrary =
    new ShellLibrary(libraryName, locationPath, overwriteExisting);

Add folder to an existing library




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.Add(folderToAdd);
}

Remove folder from a library




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.Remove(folderToRemove);
}

Enumerate library folders




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    foreach (ShellFileSystemFolder folder in shellLibrary)
    {
        Debug.WriteLine(folder.Path);
    }
}

Change default save location




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.DefaultSaveFolder = newSaveLocation;
}

Change library icon




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.IconResourceId = new IconReference(moduleName, resourceId);
}

Pin library to explorer navigation pane




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.IsPinnedToNavigationPane = true;
}

Setting library type




using (ShellLibrary shellLibrary =
    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
{
    shellLibrary.LibraryType = libraryType;
    
    // libraryType can be:
    //  LibraryFolderType.Generic
    //  LibraryFolderType.Documents
    //  LibraryFolderType.Music
    //  LibraryFolderType.Pictures
    //  LibraryFolderType.Videos
}

Open library manage UI




ShellLibrary.ShowManageLibraryUI(
    libraryName, folderPath, hOwnerWnd, title, instruction, allowNonIndexableLocations);

Delete library




string FileExtension = “.library-ms”;

File.Delete(Path.Combine(folderPath,libraryName + FileExtension));

Get library change notification




string FileExtension = “.library-ms”;

FileSystemWatcher libraryWatcher = new FileSystemWatcher(folderPath);
libraryWatcher.NotifyFilter = NotifyFilters.LastWrite;
libraryWatcher.Filter = libraryName + FileExtension;
libraryWatcher.IncludeSubdirectories = false;

libraryWatcher.Changed += (s, e) =>
{
    //cross thread call
    this.Dispatcher.Invoke(new Action(() =>
        {
            using (ShellLibrary shellLibrary =
                ShellLibrary.Load(libraryName, folderPath, isReadOnly))
            {
                // get changed information
                …
            }
        }));
};
libraryWatcher.EnableRaisingEvents = true;

That’s it for now,
Arik Poznanski.

kick it on DotNetKicks.com Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. PareshMay 15, 2011 ב 20:57

    Do you know how to “Restore Default Libraries”?