Windows 7编程新特性Shell Library接口介绍

开发 后端
这里将介绍Windows 7编程新特性Shell Library,Windows 7中引入了新的文件管理方式:库(Library),可以更快更便捷管理和搜索数据。

Windows 7的上线日期日益临近,关于Windows 7编程新特性的文章页开始出现,目前看来很多Windows 7编程新特性都是围绕.NET平台进行的,毕竟都是微软一家的东西。

下文所用到的示例代码来源于微软一站式开发技术框架解决方案。你可以通过http://cfx.codeplex.com/Release/ProjectReleases.aspx下载到Windows 7 Shell Library相关的sample。其中包含C++、C#、VB.NET对Shell Library操作的示例代码:CppWin7ShellLibrary, C#Win7ShellLibrary, VBWin7ShellLibrary。

为了帮助用户更加有效地对硬盘上的文件进行管理,Windows 7中引入了新的文件管理方式:库(Library)。库自然演化自以往操作系统中My Documents 文件夹这个概念。有了库,我们就可以将多个相关的文件夹组织到同一个库下,从而更快更便捷地管理和搜索数据。

创建Windows Shell Library

Windows 7提供了SHCreateLibrary API用来创建一个Shell Library:

  1. C++ CreateShellLibrary  
  2. /**//*!  
  3. * Create a new shell library under the user's Libraries folder. If a library   
  4. * with the same name already exists, the new one overrides the existing one.  
  5.  
  6. * \param pwszLibraryName  
  7. * The name of the shell library to be created.   
  8. */ 
  9. BOOL CreateShellLibrary(LPWSTR pwszLibraryName)  
  10. {  
  11.     /**//////////////////////////////////////////////////////////////////////////  
  12.     // Create the shell library COM object.  
  13.     //   
  14.  
  15.     IShellLibrary* pShellLib = NULL;  
  16.     HRESULT hr = SHCreateLibrary(IID_PPV_ARGS(&pShellLib));  
  17.     if (FAILED(hr))  
  18.     {  
  19.         _tprintf(_T("SHCreateLibrary failed to create the shell library ") \  
  20.             _T("COM object w/err 0x%08lx\n"), hr);  
  21.         return FALSE;  
  22.     }  
  23.  
  24.  
  25.     /**/////////////////////////////////////////////////////////////////////////  
  26.     // Save the new library under the user's Libraries folder.  
  27.     //   
  28.  
  29.     IShellItem* pSavedTo = NULL;  
  30.     hr = pShellLib->SaveInKnownFolder(FOLDERID_UsersLibraries,   
  31.         pwszLibraryName, LSF_OVERRIDEEXISTING, &pSavedTo);  
  32.     if (FAILED(hr))  
  33.     {  
  34.         _tprintf(_T("IShellLibrary::SaveInKnownFolder failed to save the ") \  
  35.             _T("library w/err 0x%08lx\n"), hr);  
  36.         return FALSE;  
  37.     }  
  38.  
  39.  
  40.     /**//////////////////////////////////////////////////////////////////////////  
  41.     // Clean up.  
  42.     //   
  43.  
  44.     if (pShellLib != NULL)  
  45.         pShellLib->Release();  
  46.  
  47.     if (pSavedTo != NULL)  
  48.         pSavedTo->Release();  
  49.  
  50.     return TRUE;  
  51. }  
  52.    
  53.  
  54. /**//////////////////////////////////////////////////////////////////////  
  55. // Create a shell library.  
  56. //   
  57.  
  58. using (ShellLibrary library = new ShellLibrary(libraryName, true))  
  59. {  

管理Windows Shell Library

你可以通过调用SHShowManageLibraryUI API显示出Windows 标准的Shell Library管理对话框。值得注意的是,在调用SHShowManageLibraryUI前请确保 shell library没有被以可写方式打开。否则在SHShowManageLibraryUI中对shell library的修改将无法被保存。

C++ ShowManageLibraryUI

  1. C++ ShowManageLibraryUI  
  2. /**//*!  
  3. * Shows the library management dialog box of the specified library, which   
  4. * enables users to manage the library folders and default save location.  
  5.  
  6. * \param pwszLibraryName  
  7. * The name of the shell library  
  8. */ 
  9. BOOL ShowManageLibraryUI(LPWSTR pwszLibraryName)  
  10. {  
  11.     // Get the shell item that represents the library.  
  12.     IShellItem2* pShellItem = GetShellLibraryItem(pwszLibraryName);  
  13.  
  14.     HRESULT hr = SHShowManageLibraryUI(pShellItem, NULL,   
  15.         L"CppWin7ShellLibrary", L"Manage Library folders and settings",   
  16.         LMD_ALLOWUNINDEXABLENETWORKLOCATIONS);  
  17.  
  18.     // Clean up  
  19.     if (pShellItem != NULL)  
  20.         pShellItem->Release();  
  21.  
  22.     return SUCCEEDED(hr);  
  23. }  
  24.  
  25.  
  26.  
  27. C# ShowManageLibraryUI  
  28. // ShowManageLibraryUI requires that the library is not currently   
  29. // opened with write permission.   
  30. ShellLibrary.ShowManageLibraryUI(libraryName, IntPtr.Zero,  
  31.     "CSWin7ShellLibrary""Manage Library folders and settings"true); 

向Shell Library中添加文件夹

SHAddFolderPathToLibrary可用来向指定的Shell Library中添加文件夹。

  1. C++ AddFolderToShellLibrary  
  2. /**//*!  
  3. * Add a folder to an existing shell library.  
  4.  
  5. * \param pShellLib  
  6. * The IShellLibrary interface of the shell library  
  7.  
  8. * \param pwszFolderPath  
  9. * The path of the folder to be added into the shell library  
  10.  
  11. * \param bSaveLocation  
  12. * If bSaveLocation is TRUE, set the folder as the save location of the shell   
  13. * library  
  14. */ 
  15. BOOL AddFolderToShellLibrary(IShellLibrary* pShellLib,   
  16.                              LPWSTR pwszFolderPath, BOOL bSaveLocation)  
  17. {  
  18.     HRESULT hr = SHAddFolderPathToLibrary(pShellLib, pwszFolderPath);  
  19.     if (FAILED(hr))  
  20.     {  
  21.         _tprintf(_T("SHAddFolderPathToLibrary failed to add a folder ") \  
  22.             _T("to the shell library w/err 0x%08lx\n"), hr);  
  23.         return FALSE;  
  24.     }  
  25.  
  26.     // Save the folder as the save location of the shell library  
  27.     if (bSaveLocation)  
  28.     {  
  29.         // Create shell item from folder path  
  30.         IShellItem2* pShellItemSaveFolder = NULL;  
  31.         hr = SHCreateItemFromParsingName(pwszFolderPath, 0,   
  32.             IID_PPV_ARGS(&pShellItemSaveFolder));  
  33.         if (FAILED(hr))  
  34.         {  
  35.             _tprintf(_T("SHCreateItemFromParsingName failed w/err ") \  
  36.                 _T("0x%08lx\n"), hr);  
  37.             return FALSE;  
  38.         }  
  39.  
  40.         // Set the folder as the save location  
  41.         pShellLib->SetDefaultSaveFolder(DSFT_DETECT, pShellItemSaveFolder);  
  42.           
  43.         if (pShellItemSaveFolder != NULL)  
  44.             pShellItemSaveFolder->Release();  
  45.  
  46.         if (FAILED(hr))  
  47.         {  
  48.             _tprintf(_T("IShellLibrary::SetDefaultSaveFolder failed ") \  
  49.                 _T("w/err 0x%08lx\n"), hr);  
  50.             return FALSE;  
  51.         }  
  52.     }  
  53.  
  54.     // Commit the change of the shell library  
  55.     pShellLib->Commit();  
  56.  
  57.     return TRUE;  
  58. }  
  59.    
  60.  
  61. C# AddFolderToShellLibrary  
  62. using (ShellLibrary library = ShellLibrary.Load(libraryName, false))  
  63. {  
  64.     /**//////////////////////////////////////////////////////////////////  
  65.     // Add a folder to the shell library.  
  66.     //   
  67.  
  68.     // Add the folder to the shell library  
  69.     library.Add(folderPath);  
  70.     library.DefaultSaveFolder = folderPath;  

枚举Shell Library中的文件夹
IShellLibrary::GetFolders可用来得到Shell Library中的文件夹。

  1. C++ ListFoldersInShellLibrary  
  2. /**//*!  
  3. * List all folders in the shell library.  
  4.  
  5. * \param pShellLib  
  6. * The IShellLibrary interface of the shell library  
  7. */ 
  8. void ListFoldersInShellLibrary(IShellLibrary* pShellLib)  
  9. {  
  10.     HRESULT hr = S_OK;  
  11.  
  12.     IShellItemArray* pShellItemArray = NULL;  
  13.     pShellLib->GetFolders(LFF_ALLITEMS, IID_PPV_ARGS(&pShellItemArray));  
  14.     if (FAILED(hr))  
  15.     {  
  16.         _tprintf(_T("IShellLibrary::GetFolders failed to get the folders ") \  
  17.             _T("of the shell library w/err 0x%08lx\n"), hr);  
  18.         return;  
  19.     }  
  20.  
  21.     DWORD dwFolderCount;  
  22.     pShellItemArray->GetCount(&dwFolderCount);  
  23.  
  24.     // Iterate through all folders of the shell library  
  25.     for (DWORD i = 0; i < dwFolderCount; i++)  
  26.     {  
  27.         IShellItem *pShellItem;  
  28.         hr = pShellItemArray->GetItemAt(i, &pShellItem);  
  29.         if (FAILED(hr))  
  30.             continue;  
  31.  
  32.         // Convert IShellItem to IShellItem2  
  33.         IShellItem2 *pShellItem2;  
  34.         pShellItem->QueryInterface(IID_PPV_ARGS(&pShellItem2));  
  35.         pShellItem->Release();  
  36.  
  37.         // Fix folder path changes  
  38.         IShellItem2 *pShellItemResolvedFolder = NULL;  
  39.         hr = pShellLib->ResolveFolder(pShellItem2, 5000, IID_PPV_ARGS(  
  40.             &pShellItemResolvedFolder));  
  41.         if (SUCCEEDED(hr))  
  42.         {  
  43.             // Point to the fixed folder  
  44.             pShellItem2->Release();  
  45.             pShellItem2 = pShellItemResolvedFolder;  
  46.         }  
  47.         // Else we will show the unfixed folder  
  48.  
  49.         // Print the folder path  
  50.         LPWSTR wszFolderPath;  
  51.         hr = pShellItem2->GetString(PKEY_ParsingPath, &wszFolderPath);  
  52.         if (SUCCEEDED(hr))  
  53.         {  
  54.             _putws(wszFolderPath);  
  55.         }  
  56.         CoTaskMemFree(wszFolderPath);  
  57.  
  58.         // Clean up  
  59.         pShellItem2->Release();  
  60.     }  
  61.  
  62.     pShellItemArray->Release();  
  63. }  
  64.  
  65.  
  66.  
  67. C# ListFoldersInShellLibrary  
  68. using (ShellLibrary library = ShellLibrary.Load(libraryName, false))  
  69. {  
  70.     /**//////////////////////////////////////////////////////////////////  
  71.     // List all folders in the library.  
  72.     //   
  73.  
  74.     foreach (ShellFolder folder in library)  
  75.     {  
  76.         Console.WriteLine(folder);  
  77.     }  

删除一个Shell Library

  1. C++ DeleteShellLibrary  
  2. /**//*!  
  3. * Delete the shell library under the user's Libraries folder according to the   
  4. * specified library name.   
  5.  
  6. * \param pwszLibraryName  
  7. * The name of the shell library to be deleted.  
  8. */ 
  9. BOOL DeleteShellLibrary(LPWSTR pwszLibraryName)  
  10. {  
  11.     /**//////////////////////////////////////////////////////////////////////////  
  12.     // Get the shell item that represents the library and its full path.  
  13.     //   
  14.  
  15.     IShellItem2* pShellItem = GetShellLibraryItem(pwszLibraryName);  
  16.  
  17.     // Get the file-system full path of the shell item  
  18.     LPWSTR wszLibraryFullPath;  
  19.     pShellItem->GetString(PKEY_ParsingPath, &wszLibraryFullPath);  
  20.  
  21.  
  22.     /**//////////////////////////////////////////////////////////////////////////  
  23.     // Delete file with the library file-system based full path.  
  24.     //   
  25.  
  26.     BOOL bSuccess = DeleteFileW(wszLibraryFullPath);  
  27.  
  28.     // Clean up  
  29.     CoTaskMemFree(wszLibraryFullPath);  
  30.     if (pShellItem != NULL)  
  31.         pShellItem->Release();  
  32.  
  33.     return bSuccess;  
  34. }  
  35.  
  36.  
  37.  
  38. C# DeleteShellLibrary  
  39. /**//////////////////////////////////////////////////////////////////////  
  40. // Delete the shell library.  
  41. //   
  42.  
  43. string librariesPath = Path.Combine(Environment.GetFolderPath(  
  44.     Environment.SpecialFolder.ApplicationData),   
  45.     ShellLibrary.LibrariesKnownFolder.RelativePath);  
  46.  
  47. string libraryPath = Path.Combine(librariesPath, libraryName);  
  48. string libraryFullPath = Path.ChangeExtension(libraryPath, "library-ms");  
  49.  
  50. File.Delete(libraryFullPath); 

Windows 7编程新特性Shell Library接口介绍就到这里吧。

原文标题:Windows 7 新特性 Shell Library 编程接口介绍

链接:http://www.cnblogs.com/Jialiang/archive/2009/09/04/Win7ShellLibrary.html

【编辑推荐】

  1. Windows 7开发与微软850位***人才无关
  2. 微软发布Windows 7开发者工具包
  3. 微软Bing由60余名印度研发人员开发
  4. 自由软件基金会怒斥OLPC已经成为微软帮凶
  5. 新的开源公司与微软签署技术授权协议
责任编辑:彭凡 来源: 博客园
相关推荐

2009-10-27 09:17:00

Windows 7编程接口

2009-08-28 08:46:15

Windows 7防火墙

2011-04-19 18:42:54

Windows Emb特性

2010-06-04 18:19:24

Windows Emb微软嵌入式Windows Emb

2009-06-21 13:28:10

2010-11-24 16:36:02

Windows PhoUI设计Windows Pho

2012-03-14 12:29:55

JavaPlay Framwo

2021-04-30 19:53:41

Java表达式代码

2013-03-25 11:34:27

Windows Blu

2010-10-08 09:54:30

IBM AIX 7

2009-05-25 08:56:26

Windows 7壁纸硬件

2009-08-18 17:03:49

C#3.5新特性

2009-08-12 13:15:44

C#3.5新特性

2011-07-06 16:38:57

Xcode Preview

2009-08-19 16:51:14

C# 4.0 dyna

2021-03-06 08:10:16

Redis6 Java架构分布式框架

2009-03-24 11:54:12

2017-01-09 16:25:55

Android Shortcuts系统

2013-04-09 12:59:21

WindowsPhon

2011-07-29 09:31:32

JDK 7
点赞
收藏

51CTO技术栈公众号