如果你对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\
2 locationPath = C:\Users\ < username > \AppData\Roaming\Microsoft\Windows\Libraries\
注意:您可以在任何地方创建库文件,不一定是在上述文件夹中。
功能:
创建一个新库:
1
ShellLibrary shellLibrary
=
2 new ShellLibrary(libraryName, locationPath, overwriteExisting);
2 new ShellLibrary(libraryName, locationPath, overwriteExisting);
添加文件夹到现有库:
1
using
(ShellLibrary shellLibrary
=
2 ShellLibrary.Load(libraryName, folderPath, isReadOnly))
3 {
4 shellLibrary.Add(folderToAdd);
5 }
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 }
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 }
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 }
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 }
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 }
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 }
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);
2 libraryName, folderPath, hOwnerWnd, title, instruction, allowNonIndexableLocations);
删除库:
1
string
FileExtension
=
"
.library-ms
"
;
2
3 File.Delete(Path.Combine(folderPath,libraryName + FileExtension));
2
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 ;
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://www.cnblogs.com/zhuqil/archive/2010/03/21/windows-7-libraries-c-quick-reference.html,如需转载请自行联系原作者