In the previous articles, we have discussed how to use Shell API to manipulate the library with unmanaged codes?
So, what about the managed codes??
To display how to manipulate the library with the managed code, we create a console application written in Visual C#, and then add the reference of Microsoft.WindowsApiCodePack.dll and Microsoft.WindowsApiCodePack.shell.dll to the project. Now, we can use the object ShellLibrary .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// use the namespace the ShellLibrary located
using Microsoft.WindowsAPICodePack.Shell;
namespace LibraryDemoCS
{
class Program
{
static void Main(string[] args)
{
// To definite the name the library and the path to save the folder
string strLibName = “MyLib”;
string strFolderPath = @”C:\”;
// create library and add the folder.
using (ShellLibrary library =new ShellLibrary( strLibName, true))
{
library.Add(strFolderPath);
}
// To load the library which already exists and perform the operation.
using (ShellLibrary lib = ShellLibrary.Load(“MyLib”, false))
{
//To add new folder
lib.Add(@”D:\”);// To set properties
lib.IsPinnedToNavigationPane = true;
string strDefSaveFolder = @”D:\”;
// To set the default folder to save the file.
lib.DefaultSaveFolder = strDefSaveFolder;
// to traverse the folder in the library in the loop.
// find and display the default folder.
foreach (ShellFolder folder in lib)
{
Console.WriteLine(“\t\t{0} {1}”, folder.Name, strDefSaveFolder ==folder.ParsingName ? “DefSaveFolder” : “”);
}
}
Console.Read();
}
}
}
In this code, we used the ShellLibrary object in Windows API Code Pack to manipulate the library such as: to create the library, to add the folder, and set the properties and traverse the folder that the library will manage, and so on. From the example, we can get to know that it is easier and more flexible to use managed code than unmanaged code.