老爷子88 发表于 2015-5-16 07:00:14

C#操作Win7/Win8的库、收藏夹

  这里说的库、收藏夹指的就是下图中左侧的部分:

  
  这里的“收藏夹”比较有迷惑性,它其实不是用户浏览器的Favorite,而是Links,可以简单理解成快捷方式。在使用方面,它有比快捷方式更强大的地方,这超出了本文所讨论的范围。
  库(Library)则是个让新用户比较迷惑的东西。可以理解为它是一个逻辑文件夹,里面包含一个或多个文件夹的快捷方式。
  

Step by Step
  操纵收藏夹,需要用到快捷方式,因此需要添加COM引用,如下图:

  之后部署的时候也要带上这个dll一起,否则运行时会报错。
  
  操纵库,需要到微软网站上下载一个SDK http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906
  下载之后解压,把Microsoft.WindowsAPICodePack.dll 和 Microsoft.WindowsAPICodePack.Shell.dll添加进工程即可。
  
  这些准备就绪之后,直接上代码(代码下载地址 http://files.iyunv.com/dc10101/Win7ShellDemo.zip ):
  
  using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using IWshRuntimeLibrary; using Microsoft.WindowsAPICodePack.Shell; namespace Win7ShellDemo { class Program { static void Main(string[] args) { LibraryDemo(); FavoriteFolderDemo(); } static void LibraryDemo() { string libraryName = "我的库"; string windowFolder = @"C:\Windows"; string programFolder = @"C:\Program Files"; ShellLibrary libCreated = new ShellLibrary(libraryName, true); // 第二个参数overwrite表示是否覆盖原有的库 libCreated.Add(windowFolder); // 添加一个目录 libCreated.Remove(windowFolder); // 删除一个已有目录 libCreated.Add(windowFolder); // 折腾一下,再添加回来 libCreated.Add(programFolder); string defaultSaveFolderPath = libCreated.DefaultSaveFolder; // 默认保存到的文件夹,是第一个添加进库的目录 libCreated.DefaultSaveFolder = programFolder;// 更改默认保存到的文件夹 libCreated.IconResourceId = new IconReference(Assembly.GetExecutingAssembly().Location, -32512); ShellLibrary libLoaded = ShellLibrary.Load(libraryName, false); // 第二个参数isReadOnly表示是否只读 // 即便不是只读,也有很多属性不能改动,例如: //libLoaded.LibraryType = LibraryFolderType.Videos; // 运行时异常 //libLoaded.IsPinnedToNavigationPane = false; // 运行时异常 // 初次新建的library可以修改这些属性 libCreated.LibraryType = LibraryFolderType.Videos; // 类型是视频 libCreated.IsPinnedToNavigationPane = false; // 不让其在左侧导航栏显示,只在右侧大的内容面板中显示 // 查看其它属性,这些属性用处不大,一般不会用到 foreach (var item in libLoaded.Properties.DefaultPropertyCollection) { Console.WriteLine(item.CanonicalName); Console.WriteLine(item.ValueType); Console.WriteLine(item.ValueAsObject); Console.WriteLine(); } } static void FavoriteFolderDemo() { string shortcutName = "我的快捷方式.lnk"; //实例化WshShell对象//通过该对象的 CreateShortcut 方法来创建 IWshShortcut 接口的实例对象string userDir = Environment.GetEnvironmentVariable(@"USERPROFILE"); string path = Path.Combine(userDir, "Links", shortcutName); WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(path); //设置快捷方式的目标所在的位置 shortcut.TargetPath = Assembly.GetExecutingAssembly().Location; //应用程序的工作目录//当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。shortcut.WorkingDirectory = System.Environment.CurrentDirectory; //目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)shortcut.WindowStyle = 1; //快捷方式的描述shortcut.Description = "鼠标停留在上面时显示的说明文字"; //可以自定义快捷方式图标.(如果不设置,则将默认为当前程序的图标.)shortcut.IconLocation = shortcut.TargetPath + ",-32512"; //设置应用程序的启动参数(如果应用程序支持的话)//shortcut.Arguments = "/myword /d4s"; //保存快捷方式shortcut.Save(); // 要删除快捷方式,在文件系统中删除这个.lnk文件即可 //System.IO.File.Delete(path); } } }
  
  上面的程序需要用到图标,所以在项目的属性页里给程序添加一个图标。

  
  可以编译运行了!
  
  建好库之后,我们可以在windows explorer里检查一下库的属性,其中打对勾的文件夹是默认的保存文件夹。

  
  查看一下刚刚创建好的快捷方式属性:

  

如何找到Icon的ResourseID

Icon的Path形如:C:\Users\lenovo\SkyDrive\project\Demo\Win7ShellDemo\Win7ShellDemo\bin\Debug\Win7ShellDemo.exe,-32512
格式为:,
要查看一个exe或dll里嵌入的资源,有一个免费而好用的工具Resource Hacker,下载页面:www.angusj.com/resourcehacker/

运行Resource Hacker后,文件菜单,打开一个exe/dll,会把内嵌的资源列出来,如下图:

  其中32512就是资源ID,注意在代码里要加一个负号,即-32512。
  

判断Windows系统版本

因为只有win7以后的版本才有库的概念,因此我们需要知道当前的系统版本:
Win7的版本号 6.1
Win8的版本号 6.2
Version version = Environment.OSVersion.Version;
if (version.Major == 6 && (version.Minor == 1 || version.Minor == 2))
{
//...
}
  

库的其它属性
  在库的Properties.DefaultPropertyCollection里包含着很多属性,但一般来说不重要,重要的属性都包含在外层了。
  注意,其中的Size属性不表示库中所有文件大小之和,亦不是数量之和,而是该.library-ms文件本身的大小,单位是字节。





属性名



属性类型



属性值





ItemFolderNameDisplay



String



Libraries





ItemTypeText



String









ItemNameDisplay



String



我的库





Size



Nullable`1



2693





FileAttributes



Nullable`1



8224





DateModified



Nullable`1



2012/7/6 9:56:40





DateCreated



Nullable`1



2012/7/6 9:35:22





DateAccessed



Nullable`1



2012/7/6 9:56:40





Document.DateCreated



Nullable`1



2012/7/6 9:35:22





Document.DateSaved



Nullable`1



2012/7/6 9:56:40





FileOwner



String



T410S\lenovo





NetworkLocation



String









ComputerName



String



T410S





ItemPathDisplayNarrow



String



我的库 (C:\用户\lenovo\AppData\Roaming\Microsoft\Windows\Libraries)





PerceivedType



Nullable`1



-2





ItemType



String



.library-ms





ParsingName



String



我的库.library-ms





SFGAOFlags



Nullable`1



4173332607





ParsingPath



String



C:\Users\lenovo\AppData\Roaming\Microsoft\Windows\Libraries\我的库.library-ms





FileExtension



String



.library-ms





ItemDate



Nullable`1



2012/7/6 9:35:22





KindText



String



文件夹





FileAttributesDisplay



String









IsShared



Nullable`1



False





SharedWith



String[]









SharingStatus



Nullable`1



2





IconPath



String



C:\Users\lenovo\SkyDrive\project\Demo\Win7ShellDemo\Win7ShellDemo\bin\Debug\Win7ShellDemo.exe,-32512





ItemName



String



我的库.library-ms





Shell.SFGAOFlagsStrings



String[]



String[]





Link.TargetSFGAOFlagsStrings



String[]









IsPinnedToNameSpaceTree



Nullable`1



False





OwnerSID



String



S-1-5-21-4070482511-544668485-4164858793-1000





OfflineAvailability



Nullable`1









Kind



String[]



String[]





Generic.Integer



Nullable`1



10





ItemFolderPathDisplayNarrow



String



Libraries (C:\用户\lenovo\AppData\Roaming\Microsoft\Windows)





FileName



String



我的库





ThumbnailCacheId



Nullable`1



5447526144864013365





Link.TargetParsingPath



String









Link.TargetSFGAOFlags



Nullable`1









ItemFolderPathDisplay



String



C:\用户\lenovo\AppData\Roaming\Microsoft\Windows\Libraries





ItemPathDisplay



String



C:\用户\lenovo\AppData\Roaming\Microsoft\Windows\Libraries\我的库.library-ms









Object



我的库





Link.TargetExtension



String[]









OfflineStatus



Nullable`1









IsFolder



Nullable`1



False





DateImported



Nullable`1



2012/7/6 9:35:22





ItemFolderNameDisplay



String



Libraries




页: [1]
查看完整版本: C#操作Win7/Win8的库、收藏夹