DCSIMG
Windows 7 Libraries C# Quick Reference - Arik Poznanski's Blog

Arik Poznanski's Blog

It CAN be done with .NET

News

MVP

MCC

CodeProject MVP

MCPD

MCTS

Subscribe to my blog by email

Arik Poznanski LinkedIn Profile

Email: arik.com at gmail dot com
or, use this form

Locations of visitors to this page


Sela Group

Sela Canada

DZone MVB

Links

Official Blogs

WPF / SL Blogs

Developers Blogs

Windows 7 Libraries C# Quick Reference

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

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# March 15, 2010 1:34 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 15, 2010 1:34 AM

Physical and Engineering Sciences News ?? Recent Article from NCSU … | Engineering Physics Science Applied said:

Pingback from  Physical and Engineering Sciences News ?? Recent Article from NCSU &#8230; | Engineering Physics Science Applied

# March 15, 2010 1:44 PM

iAwaaz-News-by-People said:

Thank you for submitting this cool story - Trackback from iAwaaz-News-by-People

# March 16, 2010 3:56 AM

Windows 7 Libraries C# Quick Reference – Arik Poznanski's Blog | C# WebDev Insider said:

Pingback from  Windows 7 Libraries C# Quick Reference &#8211; Arik Poznanski&#039;s Blog | C# WebDev Insider

# March 16, 2010 12:42 PM

Tips on how to Cook Baked Potato on a BBQ Grill | Martial Arts Leisure Knowledge said:

Pingback from  Tips on how to Cook Baked Potato on a BBQ Grill | Martial Arts Leisure Knowledge

# March 18, 2010 12:39 AM

uberVU - social comments said:

This post was mentioned on Twitter by iawaaz: NEW: Windows 7 Libraries C# Quick Reference - Arik Poznanski's Blog - http://is.gd/aJ8rH,#libraries #Windows7 #Windows7CodePack

# March 18, 2010 6:04 PM

????????????????????? Blog» Windows 7??????Library??????C#???????????? said:

Pingback from  ????????????????????? Blog&raquo; Windows 7??????Library??????C#????????????

# April 29, 2010 8:27 AM

Paresh said:

Do you know how to "Restore Default Libraries"?

# May 15, 2011 8:57 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: