Microsoft Study Bible

October 16, 2009

How to perform operation on library with managed code.

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.

How to manipulate the library with unmanaged codes?

We could use the library Shell API in Windows 7 and unmanaged codes to manage the libraries .When downloading the software, we often used the folder to classify kinds of resources. For example, among those software, the resource which were downloaded were organized in “All downloads” .In fact, those resources could be stored in different directories and partitions on your hard disk. To be more conveniently use Windows-Explorer to access all of those resources, there is need to create a “MyDownload” library, which is corresponding to the Classified Management way of the “All downloads” file, and manage all of resources downloaded in.

   To better display how to manipulate the library with Shell API, we create a simple console application with Visual C++. In the main function, we’ll create and do the operation of library.

 #include “stdafx.h”
// the introduction of header files

#include <shobjidl.h> // introduce Shell API
#include <objbase.h>
// define  IID_PPV_ARGS  macro
#include <Knownfolders.h> // introduce FOLDERID

int _tmain(int argc, _TCHAR* argv[])
{
    
// COM initialization
    CoInitialize(NULL);
    
// use Shell API to create a library

    IShellLibrary *pIShelLibrary;
    HRESULT hr = SHCreateLibrary(IID_PPV_ARGS(&pIShelLibrary));
    
if (SUCCEEDED(hr))
    {
        
// if the library are successfully created,add the paths of the different files to it.

        IShellItem *pIShellItem;
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“C:\\Users\\Public\\Pictures”);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“C:\\Users\\Public\\Music”
);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“D:\\Tools”
);
        SHAddFolderPathToLibrary(pIShelLibrary,
        L
“D:\\Video”
);
        
// store current library into the directories of the system library .
        // that is to say ,to add a new library
MyDownload

        hr = pIShelLibrary->SaveInKnownFolder(FOLDERID_Libraries ,
        L
“MyDownload”
,
        LSF_MAKEUNIQUENAME,
        &pIShellItem);
      
// release the object.
       pIShellItem->Release();
       pIShelLibrary->Release();
  }
    
// release COM

    ::CoUninitialize();
    
return 0;

 

In this code, at first, we introduce the header files that Shell API required .Then in the main function, because those APIs all are based on .COM, we should firstly do COM initialization .After initialization, we can do the operation of the libraries by Shell API. In the code example, we create a new library object with SHCreateLibrary function, and use SHAddFolderPathToLibrary function to add the paths on the hard disk into the library, that is, to use the library to manage the files under these paths. Afterwards, we stored this library created into FOLDERID_Libraries, that is, create a new library definition file under this directoryFinally, we need to release the object COM .After the above steps, we could see the library created in the file browser.

Powered by WordPress

Close
E-mail It