自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows 7編程新特性Shell Library接口介紹

開發(fā) 后端
這里將介紹Windows 7編程新特性Shell Library,Windows 7中引入了新的文件管理方式:庫(Library),可以更快更便捷管理和搜索數(shù)據(jù)。

Windows 7的上線日期日益臨近,關(guān)于Windows 7編程新特性的文章頁開始出現(xiàn),目前看來很多Windows 7編程新特性都是圍繞.NET平臺(tái)進(jìn)行的,畢竟都是微軟一家的東西。

下文所用到的示例代碼來源于微軟一站式開發(fā)技術(shù)框架解決方案。你可以通過http://cfx.codeplex.com/Release/ProjectReleases.aspx下載到Windows 7 Shell Library相關(guān)的sample。其中包含C++、C#、VB.NET對(duì)Shell Library操作的示例代碼:CppWin7ShellLibrary, C#Win7ShellLibrary, VBWin7ShellLibrary。

為了幫助用戶更加有效地對(duì)硬盤上的文件進(jìn)行管理,Windows 7中引入了新的文件管理方式:庫(Library)。庫自然演化自以往操作系統(tǒng)中My Documents 文件夾這個(gè)概念。有了庫,我們就可以將多個(gè)相關(guān)的文件夾組織到同一個(gè)庫下,從而更快更便捷地管理和搜索數(shù)據(jù)。

創(chuàng)建Windows Shell Library

Windows 7提供了SHCreateLibrary API用來創(chuàng)建一個(gè)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

你可以通過調(diào)用SHShowManageLibraryUI API顯示出Windows 標(biāo)準(zhǔn)的Shell Library管理對(duì)話框。值得注意的是,在調(diào)用SHShowManageLibraryUI前請(qǐng)確保 shell library沒有被以可寫方式打開。否則在SHShowManageLibraryUI中對(duì)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.     }  

刪除一個(gè)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接口介紹就到這里吧。

原文標(biāo)題:Windows 7 新特性 Shell Library 編程接口介紹

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

【編輯推薦】

  1. Windows 7開發(fā)與微軟850位***人才無關(guān)
  2. 微軟發(fā)布Windows 7開發(fā)者工具包
  3. 微軟Bing由60余名印度研發(fā)人員開發(fā)
  4. 自由軟件基金會(huì)怒斥OLPC已經(jīng)成為微軟幫兇
  5. 新的開源公司與微軟簽署技術(shù)授權(quán)協(xié)議
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

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設(shè)計(jì)Windows Pho

2021-04-30 19:53:41

Java表達(dá)式代碼

2012-03-14 12:29:55

JavaPlay Framwo

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壁紙硬件

2017-01-09 16:25:55

Android Shortcuts系統(tǒng)

2009-03-24 11:54:12

2021-03-06 08:10:16

Redis6 Java架構(gòu)分布式框架

2009-08-19 16:51:14

C# 4.0 dyna

2009-08-12 13:15:44

C#3.5新特性

2009-08-18 17:03:49

C#3.5新特性

2011-07-06 16:38:57

Xcode Preview

2013-04-09 12:59:21

WindowsPhon

2009-12-23 18:58:51

Fedora 7
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)