C#操作Win7/Win8的庫、收藏夾


這里說的庫、收藏夾指的就是下圖中左側的部分:

image

 

這里的“收藏夾”比較有迷惑性,它其實不是用戶瀏覽器的Favorite,而是Links,可以簡單理解成快捷方式。在使用方面,它有比快捷方式更強大的地方,這超出了本文所討論的范圍。

庫(Library)則是個讓新用戶比較迷惑的東西。可以理解為它是一個邏輯文件夾,里面包含一個或多個文件夾的快捷方式。

 

Step by Step

操縱收藏夾,需要用到快捷方式,因此需要添加COM引用,如下圖:

image

之后部署的時候也要帶上這個dll一起,否則運行時會報錯。

 

操縱庫,需要到微軟網站上下載一個SDK http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906

下載之后解壓,把Microsoft.WindowsAPICodePack.dll 和 Microsoft.WindowsAPICodePack.Shell.dll添加進工程即可。

 

這些准備就緒之后,直接上代碼(代碼下載地址 http://files.cnblogs.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); } } }

 

上面的程序需要用到圖標,所以在項目的屬性頁里給程序添加一個圖標。

image

 

可以編譯運行了!

 

建好庫之后,我們可以在windows explorer里檢查一下庫的屬性,其中打對勾的文件夾是默認的保存文件夾。

image

 

查看一下剛剛創建好的快捷方式屬性:

image

 

如何找到Icon的ResourseID

Icon的Path形如:C:\Users\lenovo\SkyDrive\project\Demo\Win7ShellDemo\Win7ShellDemo\bin\Debug\Win7ShellDemo.exe,-32512

格式為:<exe或dll路徑名>,<ResourceID>

要查看一個exe或dll里嵌入的資源,有一個免費而好用的工具Resource Hacker,下載頁面:www.angusj.com/resourcehacker/

 

運行Resource Hacker后,文件菜單,打開一個exe/dll,會把內嵌的資源列出來,如下圖:

image

其中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[UInt64]

2693

FileAttributes

Nullable`1[UInt32]

8224

DateModified

Nullable`1[DateTime]

2012/7/6 9:56:40

DateCreated

Nullable`1[DateTime]

2012/7/6 9:35:22

DateAccessed

Nullable`1[DateTime]

2012/7/6 9:56:40

Document.DateCreated

Nullable`1[DateTime]

2012/7/6 9:35:22

Document.DateSaved

Nullable`1[DateTime]

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[Int32]

-2

ItemType

String

.library-ms

ParsingName

String

我的庫.library-ms

SFGAOFlags

Nullable`1[UInt32]

4173332607

ParsingPath

String

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

FileExtension

String

.library-ms

ItemDate

Nullable`1[DateTime]

2012/7/6 9:35:22

KindText

String

文件夾

FileAttributesDisplay

String

 

IsShared

Nullable`1[Boolean]

False

SharedWith

String[]

 

SharingStatus

Nullable`1[UInt32]

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[Boolean]

False

OwnerSID

String

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

OfflineAvailability

Nullable`1[UInt32]

 

Kind

String[]

String[]

Generic.Integer

Nullable`1[UInt32]

10

ItemFolderPathDisplayNarrow

String

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

FileName

String

我的庫

ThumbnailCacheId

Nullable`1[UInt64]

5447526144864013365

Link.TargetParsingPath

String

 

Link.TargetSFGAOFlags

Nullable`1[UInt32]

 

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[UInt32]

 

IsFolder

Nullable`1[Boolean]

False

DateImported

Nullable`1[DateTime]

2012/7/6 9:35:22

ItemFolderNameDisplay

String

Libraries


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM