天天看點

Windows 7庫(Library):C#快速參考

這篇文章中的代碼來自Alon和Sela工作小組的成員。

每個Windows 7庫用一個XML檔案表示,擴充名為.library-ms。

通用庫檔案通常存儲在:C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\。

例如,我們現在使用圖檔庫,如以下代碼:

1 libraryName = Pictures 

2 locationPath = C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Libraries\

注意:您可以在任何地方建立庫檔案,不一定是在上述檔案夾中。

功能:

建立一個新庫:

1 ShellLibrary shellLibrary = 

2     new ShellLibrary(libraryName, locationPath, overwriteExisting);

添加檔案夾到現有庫:

1 using (ShellLibrary shellLibrary =

2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))

3 {

4     shellLibrary.Add(folderToAdd);

5 }

從庫中删除檔案夾:

4     shellLibrary.Remove(folderToRemove);

枚舉庫檔案夾:

<a></a>

1 using (ShellLibrary shellLibrary = 

4     foreach (ShellFileSystemFolder folder in shellLibrary)

5     {

6         Debug.WriteLine(folder.Path);

7     }

8 }

更改預設儲存位置: 

4     shellLibrary.DefaultSaveFolder = newSaveLocation;

更改庫圖示:

4     shellLibrary.IconResourceId = new IconReference(moduleName, resourceId);

鎖住庫浏覽導航窗格:

4     shellLibrary.IsPinnedToNavigationPane = true;

設定庫的類型:

 1 using (ShellLibrary shellLibrary = 

 2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))

 3 {

 4     shellLibrary.LibraryType = libraryType;

 5     

 6     // libraryType can be:

 7     //  LibraryFolderType.Generic

 8     //  LibraryFolderType.Documents

 9     //  LibraryFolderType.Music

10     //  LibraryFolderType.Pictures

11     //  LibraryFolderType.Videos

12 }

打開庫管理界面:

1 ShellLibrary.ShowManageLibraryUI(

2     libraryName, folderPath, hOwnerWnd, title, instruction, allowNonIndexableLocations);

删除庫:

1 string FileExtension = ".library-ms";

3 File.Delete(Path.Combine(folderPath,libraryName + FileExtension));

擷取庫的更改通知:

 1 string FileExtension = ".library-ms";

 2 

 3 FileSystemWatcher libraryWatcher = new FileSystemWatcher(folderPath);

 4 libraryWatcher.NotifyFilter = NotifyFilters.LastWrite;

 5 libraryWatcher.Filter = libraryName + FileExtension;

 6 libraryWatcher.IncludeSubdirectories = false;

 7 

 8 libraryWatcher.Changed += (s, e) =&gt;

 9 {

10     //cross thread call

11     this.Dispatcher.Invoke(new Action(() =&gt;

12         {

13             using (ShellLibrary shellLibrary = 

14                 ShellLibrary.Load(libraryName, folderPath, isReadOnly))

15             {

16                 // get changed information

17                 ...

18             }

19         }));

20 };

21 libraryWatcher.EnableRaisingEvents = true;

本文轉自麒麟部落格園部落格,原文連結:http://www.cnblogs.com/zhuqil/archive/2010/03/21/windows-7-libraries-c-quick-reference.html,如需轉載請自行聯系原作者