Windows 7库(Library):C#快速参考

本文详细介绍了Windows7库的概念、功能及其使用方法,包括创建、添加、删除文件夹,枚举库文件夹,更改默认保存位置、图标、浏览导航窗格等操作,并提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你对windows7库的概念不了解,请先看这篇介绍:Windows 7新功能:库(Library)

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

这篇文章中的代码来自Alon和Sela工作小组的成员。

每个Windows 7库用一个XML文件表示,扩展名为.library-ms。

通用库文件通常存储在:C:\Users\<username>\AppData\Roaming\Microsoft \Windows\Libraries\。

例如,我们现在使用图片库,如以下代码:
  1. 1 libraryName = Pictures 
  2. 2 locationPath = C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\
复制代码
注意:您可以在任何地方创建库文件,不一定是在上述文件夹中。

功能:

创建一个新库:
  1. 1 ShellLibrary shellLibrary = 
  2. 2    new ShellLibrary(libraryName, locationPath, overwriteExisting);
复制代码
添加文件夹到现有库:
  1. 1 using (ShellLibrary shellLibrary =
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.Add(folderToAdd);
  5. 5 }
复制代码
从库中删除文件夹:
  1. 1 using (ShellLibrary shellLibrary =
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.Remove(folderToRemove);
  5. 5 }
复制代码
枚举库文件夹:
  1. 1 using (ShellLibrary shellLibrary = 
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    foreach (ShellFileSystemFolder folder in shellLibrary)
  5. 5    {
  6. 6        Debug.WriteLine(folder.Path);
  7. 7    }
  8. 8 }
复制代码
更改默认保存位置:
  1. 1 using (ShellLibrary shellLibrary = 
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.DefaultSaveFolder = newSaveLocation;
  5. 5 }
复制代码
更改库图标:
  1. 1 using (ShellLibrary shellLibrary = 
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.IconResourceId = new IconReference(moduleName, resourceId);
  5. 5 }
复制代码
锁住库浏览导航窗格:
  1. 1 using (ShellLibrary shellLibrary = 
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.IsPinnedToNavigationPane = true;
  5. 5 }
复制代码
设置库的类型:
  1. 1 using (ShellLibrary shellLibrary = 
  2. 2    ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  3. 3 {
  4. 4    shellLibrary.LibraryType = libraryType;
  5. 5    
  6. 6    // libraryType can be:
  7. 7    //  LibraryFolderType.Generic
  8. 8    //  LibraryFolderType.Documents
  9. 9    //  LibraryFolderType.Music
  10. 10    //  LibraryFolderType.Pictures
  11. 11    //  LibraryFolderType.Videos
  12. 12 }
复制代码
打开库管理界面:
  1. 1 ShellLibrary.ShowManageLibraryUI(
  2. 2    libraryName, folderPath, hOwnerWnd, title, instruction, allowNonIndexableLocations);
复制代码
删除库:
  1. 1 string FileExtension = ".library-ms";

  2. 3 File.Delete(Path.Combine(folderPath,libraryName + FileExtension));
复制代码
获取库的更改通知:
  1. 1 string FileExtension = ".library-ms";

  2. 3 FileSystemWatcher libraryWatcher = new FileSystemWatcher(folderPath);
  3. 4 libraryWatcher.NotifyFilter = NotifyFilters.LastWrite;
  4. 5 libraryWatcher.Filter = libraryName + FileExtension;
  5. 6 libraryWatcher.IncludeSubdirectories = false;

  6. 8 libraryWatcher.Changed += (s, e) =>
  7. 9 {
  8. 10    //cross thread call
  9. 11    this.Dispatcher.Invoke(new Action(() =>
  10. 12        {
  11. 13            using (ShellLibrary shellLibrary = 
  12. 14                ShellLibrary.Load(libraryName, folderPath, isReadOnly))
  13. 15            {
  14. 16                // get changed information
  15. 17                ...
  16. 18            }
  17. 19        }));
  18. 20 };
  19. 21 libraryWatcher.EnableRaisingEvents = true;
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值