Windows 7库(Library):C#快速参考
如果你对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;
(全文完)
以下为广告部分
您部署的HTTPS网站安全吗?
如果您想看下您的网站HTTPS部署的是否安全,花1分钟时间来 myssl.com 检测以下吧。让您的HTTPS网站变得更安全!
快速了解HTTPS网站安全情况。
安全评级(A+、A、A-...)、行业合规检测、证书信息查看、证书链信息以及补完、服务器套件信息、证书兼容性检测等。
安装部署SSL证书变得更方便。
SSL证书内容查看、SSL证书格式转换、CSR在线生成、SSL私钥加解密、CAA检测等。
让服务器远离SSL证书漏洞侵扰
TLS ROBOT漏洞检测、心血漏洞检测、FREAK Attack漏洞检测、SSL Poodle漏洞检测、CCS注入漏洞检测。
作者:朱祁林
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。