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

來源:互聯網
上載者:User

如果你對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 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 }

從庫中刪除檔案夾:

1 using (ShellLibrary shellLibrary =
2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))
3 {
4     shellLibrary.Remove(folderToRemove);
5 }

枚舉庫檔案夾:

1 using (ShellLibrary shellLibrary = 
2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))
3 {
4     foreach (ShellFileSystemFolder folder in shellLibrary)
5     {
6         Debug.WriteLine(folder.Path);
7     }
8 }

更改預設儲存位置: 

1 using (ShellLibrary shellLibrary = 
2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))
3 {
4     shellLibrary.DefaultSaveFolder = newSaveLocation;
5 }

更改庫表徵圖:

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

鎖住庫瀏覽瀏覽窗格:

1 using (ShellLibrary shellLibrary = 
2     ShellLibrary.Load(libraryName, folderPath, isReadOnly))
3 {
4     shellLibrary.IsPinnedToNavigationPane = true;
5 }

設定庫的類型:

 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) =>
 9 {
10     //cross thread call
11     this.Dispatcher.Invoke(new Action(() =>
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://blogs.microsoft.co.il/blogs/arik/archive/2010/03/15/windows-7-libraries-c-quick-reference.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.