Click here to Skip to main content
16,016,527 members
Articles / All Topics

Windows 7 Libraries C# Quick Reference

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
16 Mar 2010Ms-PL 36.7K   16   5
Following are some listings to be used as a quick reference to common Windows 7 Libraries features using Windows API Code Pack.

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.

This article was originally posted at http://feeds.feedburner.com/ArikPoznanskisBlog

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
NewsShellLibrary.Dispose() Pin
Focuscar15-Mar-11 16:02
Focuscar15-Mar-11 16:02 
QuestionGet Library Change notification - questions Pin
freestone_girl28-Feb-11 23:22
freestone_girl28-Feb-11 23:22 
Hi Arik, and thanks for this post.

I have a few questions related to the last part of your post. You added a FileSystemWatcher to a folderPath - is that the Libraries folder (C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\) or does that refer to the default save folder for the library?

I tried setting a watcher for the Libraries folder mentioned above, and I've encountered a few problems:
* The file extension filter does not work; whenever there are changes in the libraries, the files that are changed ususally have .TMP extention
* The rename & delete library actions are not caught by the watcher ... only adding, removing or renaming folders to existing libraries, or adding new libraries are caught by the watcher. I've tried adding more NotifyFilters, or catching other events than the Changed event, but to no avail.

Any help would be greatly appreciated.

Thank you.
GeneralNice article. Pin
ybkishore30-Dec-10 17:12
ybkishore30-Dec-10 17:12 
GeneralRe: Nice article. Pin
Arik Poznanski1-Jan-11 13:17
Arik Poznanski1-Jan-11 13:17 
Generalmy 5 Pin
Southmountain25-Nov-10 11:28
Southmountain25-Nov-10 11:28 

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.