Lisong's Blog


三月 22nd, 2010

C# Windows7库(Library)快速参考

Learning, by 木公.

以下是一些常见的Windows7库功能快速参考,使用了Windows API Code Pack

这篇文章中的基础代码来自Alon和Sela小组成员,了不起的成果。

前瞻

需要说明,每个Windows7库用一个XML文件表示,扩展名为.library-ms。

通用库的文件存储在:C:\Users\{username}\AppData\Roaming\Microsoft \Windows\Libraries\。

例如,我们使用的图片库,使用如下代码:

libraryName = Pictures
locationPath = C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Libraries\

注意:可以在任何地方创建库,不一定限于上述文件夹。

功能

创建一个新库:

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

添加文件夹到现有的库:

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

将文件夹从库中删除:

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

枚举库文件夹:

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

更改默认保存位置:

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

更改库图标:

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

锁定库导航窗格:

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

设置库类型:

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

打开库管理界面:

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

删除库:

string FileExtension = ".library-ms";
File.Delete(Path.Combine(folderPath,libraryName + FileExtension));

获取库更改通知:

string FileExtension = ".library-ms";
 
FileSystemWatcher libraryWatcher = new FileSystemWatcher(folderPath);
libraryWatcher.NotifyFilter = NotifyFilters.LastWrite;
libraryWatcher.Filter = libraryName + FileExtension;
libraryWatcher.IncludeSubdirectories = false;
 
libraryWatcher.Changed += (s, e) =>
{
    // 跨线程调用
    this.Dispatcher.Invoke(new Action(() =>
        {
            using (ShellLibrary shellLibrary =
                ShellLibrary.Load(libraryName, folderPath, isReadOnly))
            {
                // 获得更改后的消息
                ...
            }
        }));
};
libraryWatcher.EnableRaisingEvents = true;

Back Top

回复自“C# Windows7库(Library)快速参考”

评论 (0) 引用 (0) 发表评论 引用地址
  1. 没有任何评论。
  1. 没有任何引用。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


无觅相关文章插件,快速提升流量