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.

September 16, 2009

How to perform operations on libraries in Windows 7?

After creating the library, we’ll use Shell API to perform operations on libraries, such as to set the icon of the library, and the default path to save the library, or enumerate all of the folders in the library, and so on.

 

  #include “stdafx.h”
#include <shobjidl.h>
#include <objbase.h>
// IID_PPV_ARGS macro
#include <Knownfolders.h>
#include <Shlguid.h.>
int _tmain(int argc, _TCHAR* argv[])
{
   CoInitialize(NULL);
    
// Create the library
    //manipulate the library

    IShellLibrary *pslLibrary;
    
// load the library that already exists and manage it .
    HRESULT hr = SHLoadLibraryFromParsingName(L
“C:\\Users\\Win7\\
    AppData\\Roaming\\Microsoft\\Windows\\Libraries\\
    MyDownload.library-ms
“,
    STGM_READWRITE, IID_PPV_ARGS(&pslLibrary));
    
if(SUCCEEDED(hr))
    {
        
// To set the icon of the library

        hr = pslLibrary->SetIcon(L
“C:\\Windows\\System32\\SHELL32.dll,-14″);
        
// To set the type of the library

        hr = pslLibrary->SetFolderType(FOLDERTYPEID_GenericLibrary);
        
//traverse all folders in the library in the loop.
        IShellItemArray *psiaFolders;
        hr = pslLibrary->GetFolders(LFF_STORAGEITEMS,
        IID_PPV_ARGS(&psiaFolders));
        IEnumShellItems *penumShellItems;
        psiaFolders->EnumItems(&penumShellItems);
        DWORD dwCount =
0
;
        psiaFolders->GetCount(&dwCount);
        IShellItem *psiFolder;
      
// traverse all folders the library managed in the loop.
       for(DWORD dwIndex = 0
; dwIndex < dwCount; ++dwIndex )
            {
                
// To obtain the folder

                 psiaFolders->GetItemAt(dwIndex, &psiFolder );
                 WCHAR strFolderName[
256] = L“”;
                  LPWSTR *pName = (LPWSTR*)strFolderName;
                  
// To obtain the name of the folder.


                  hr = psiFolder->GetDisplayName(SIGDN_NORMALDISPLAY,

                 (LPWSTR*)pName);
                 if(SUCCEEDED(hr))
                 {
                
// To compare the name of the folder with the “Tools”
                 // That is ,to find the folder named “Tools”

 

                     if(wcscmp( *pName, L“Tools”) == 0)
                     {
                    
// if the “Tools ”folder could be found ,

//and the folder would be set the default folder for the library to save the file.
                   hr = pslLibrary- >SetDefaultSaveFolder(DSFT_PRIVATE, psiFolder);
                      }
                  }
              }
              
// To submit the changes to the library.
          pslLibrary->Commit();
          pslLibrary->Release();
          }
    }
        ::CoUninitialize();
    
return 0
;
}

In this code, we firstly use SHLoadLibraryFromParsingName function to load the library from its definition file and create an IShellLibrary object.

Then we could do the operation on the library with the operation functions provided by the IShellLibrary object. In the code, we modified the icon of the library by using SetIcon function, which accepted a string as a parameter and assigned the name of DLL and Icon Index.

Afterwards we modified the type of the library by using SetFolderType function ,which could accept GUID as the parameter ,which defined the types of the library ,which could be one of the following types: Generic types, picture, music, video, document and so on. When setting the type of library, we can change the Windows explorer view of the library, and enable the search and the view options specially designated for the library type. The next step is to traverse all folders in the library in the loop and find the folder named “Tools” and the folder will be set the default folder for the library to save the file. By default, when we chose a library to save the files in CFD, the system would use the first folder to save the files .However; we could re-specify the default location of the library by modifying the default folder. At last, we can call Commit function to submit the changes to the library, and that is, to write these changes to the library definition files, in order to complete the operation on the library. 

 

 

 

 

 

Powered by WordPress

Close
E-mail It