Windows 7 新特性 Shell Library 编程接口介绍

你可以通过  http://cfx.codeplex.com/Release/ProjectReleases.aspx下载到Windows 7 Shell Library相关的sample。其中包含C++、C#、VB.NET对Shell Library操作的示例代码:CppWin7ShellLibrary, C#Win7ShellLibrary, VBWin7ShellLibrary。

  为了帮助用户更加有效地对硬盘上的文件进行管理,Windows 7中引入了新的文件管理方式:库(Library)。库自然演化自以往操作系统中My Documents 文件夹这个概念。有了库,我们就可以将多个相关的文件夹组织到同一个库下,从而更快更便捷地管理和搜索数据。

 

  创建Windows Shell Library

  Windows 7提供了SHCreateLibrary API用来创建一个Shell Library:

  C++ CreateShellLibrary

/**//*!
* Create a new shell library under the user's Libraries folder. If a library
* with the same name already exists, the new one overrides the existing one.
*
* \param pwszLibraryName
* The name of the shell library to be created.
*/
BOOL CreateShellLibrary(LPWSTR pwszLibraryName)
{
    /**//
    // Create the shell library COM object.
    //

    IShellLibrary* pShellLib = NULL;
    HRESULT hr = SHCreateLibrary(IID_PPV_ARGS(&pShellLib));
    if (FAILED(hr))
    {
        _tprintf(_T("SHCreateLibrary failed to create the shell library ") \
            _T("COM object w/err 0x%08lx\n"), hr);
        return FALSE;
    }


    /**/
    // Save the new library under the user's Libraries folder.
    //

    IShellItem* pSavedTo = NULL;
    hr = pShellLib->SaveInKnownFolder(FOLDERID_UsersLibraries,
        pwszLibraryName, LSF_OVERRIDEEXISTING, &pSavedTo);
    if (FAILED(hr))
    {
        _tprintf(_T("IShellLibrary::SaveInKnownFolder failed to save the ") \
            _T("library w/err 0x%08lx\n"), hr);
        return FALSE;
    }


    /**//
    // Clean up.
    //

    if (pShellLib != NULL)
        pShellLib->Release();

    if (pSavedTo != NULL)
        pSavedTo->Release();

    return TRUE;
}
 

/**//
// Create a shell library.
//

using (ShellLibrary library = new ShellLibrary(libraryName, true))
{
}

  管理Windows Shell Library

你可以通过调用SHShowManageLibraryUI API显示出Windows 标准的Shell Library管理对话框。值得注意的是,在调用SHShowManageLibraryUI前请确保 shell library没有被以可写方式打开。否则在SHShowManageLibraryUI中对shell library的修改将无法被保存。

  

 

  C++ ShowManageLibraryUI

/**//*!
* Shows the library management dialog box of the specified library, which
* enables users to manage the library folders and default save location.
*
* \param pwszLibraryName
* The name of the shell library
*/
BOOL ShowManageLibraryUI(LPWSTR pwszLibraryName)
{
    // Get the shell item that represents the library.
    IShellItem2* pShellItem = GetShellLibraryItem(pwszLibraryName);

    HRESULT hr = SHShowManageLibraryUI(pShellItem, NULL,
        L"CppWin7ShellLibrary", L"Manage Library folders and settings",
        LMD_ALLOWUNINDEXABLENETWORKLOCATIONS);

    // Clean up
    if (pShellItem != NULL)
        pShellItem->Release();

    return SUCCEEDED(hr);
}

  C# ShowManageLibraryUI

// ShowManageLibraryUI requires that the library is not currently
// opened with write permission.
ShellLibrary.ShowManageLibraryUI(libraryName, IntPtr.Zero,
    "CSWin7ShellLibrary", "Manage Library folders and settings", true);

  向Shell Library中添加文件夹

  SHAddFolderPathToLibrary可用来向指定的Shell Library中添加文件夹。

  C++ AddFolderToShellLibrary

/**//*!
* Add a folder to an existing shell library.
*
* \param pShellLib
* The IShellLibrary interface of the shell library
*
* \param pwszFolderPath
* The path of the folder to be added into the shell library
*
* \param bSaveLocation
* If bSaveLocation is TRUE, set the folder as the save location of the shell
* library
*/
BOOL AddFolderToShellLibrary(IShellLibrary* pShellLib,
                             LPWSTR pwszFolderPath, BOOL bSaveLocation)
{
    HRESULT hr = SHAddFolderPathToLibrary(pShellLib, pwszFolderPath);
    if (FAILED(hr))
    {
        _tprintf(_T("SHAddFolderPathToLibrary failed to add a folder ") \
            _T("to the shell library w/err 0x%08lx\n"), hr);
        return FALSE;
    }

    // Save the folder as the save location of the shell library
    if (bSaveLocation)
    {
        // Create shell item from folder path
        IShellItem2* pShellItemSaveFolder = NULL;
        hr = SHCreateItemFromParsingName(pwszFolderPath, 0,
            IID_PPV_ARGS(&pShellItemSaveFolder)); 
 

        if (FAILED(hr))
        {
            _tprintf(_T("SHCreateItemFromParsingName failed w/err ") \
                _T("0x%08lx\n"), hr);
            return FALSE;
        }

        // Set the folder as the save location
        pShellLib->SetDefaultSaveFolder(DSFT_DETECT, pShellItemSaveFolder);
       
        if (pShellItemSaveFolder != NULL)
            pShellItemSaveFolder->Release();

        if (FAILED(hr))
        {
            _tprintf(_T("IShellLibrary::SetDefaultSaveFolder failed ") \
                _T("w/err 0x%08lx\n"), hr);
            return FALSE;
        }
    }

    // Commit the change of the shell library
    pShellLib->Commit();

    return TRUE;
}
  C# AddFolderToShellLibrary

using (ShellLibrary library = ShellLibrary.Load(libraryName, false))
{
    /**//
    // Add a folder to the shell library.
    //

    // Add the folder to the shell library
    library.Add(folderPath);
    library.DefaultSaveFolder = folderPath;
}

  枚举Shell Library中的文件夹

  IShellLibrary::GetFolders可用来得到Shell Library中的文件夹。

  C++ ListFoldersInShellLibrary

/**//*!
* List all folders in the shell library.
*
* \param pShellLib
* The IShellLibrary interface of the shell library
*/
void ListFoldersInShellLibrary(IShellLibrary* pShellLib)
{
    HRESULT hr = S_OK;

    IShellItemArray* pShellItemArray = NULL;
    pShellLib->GetFolders(LFF_ALLITEMS, IID_PPV_ARGS(&pShellItemArray));
    if (FAILED(hr))
    {
        _tprintf(_T("IShellLibrary::GetFolders failed to get the folders ") \
            _T("of the shell library w/err 0x%08lx\n"), hr);
        return;
    }

    DWORD dwFolderCount;
    pShellItemArray->GetCount(&dwFolderCount);

    // Iterate through all folders of the shell library
    for (DWORD i = 0; i < dwFolderCount; i++)
    {
        IShellItem *pShellItem;
        hr = pShellItemArray->GetItemAt(i, &pShellItem);
        if (FAILED(hr))
            continue;

        // Convert IShellItem to IShellItem2
        IShellItem2 *pShellItem2;
        pShellItem->QueryInterface(IID_PPV_ARGS(&pShellItem2));
        pShellItem->Release();

        // Fix folder path changes
        IShellItem2 *pShellItemResolvedFolder = NULL;

hr = pShellLib->ResolveFolder(pShellItem2, 5000, IID_PPV_ARGS(
            &pShellItemResolvedFolder));
        if (SUCCEEDED(hr))
        {
            // Point to the fixed folder
            pShellItem2->Release();
            pShellItem2 = pShellItemResolvedFolder;
        }
        // Else we will show the unfixed folder

        // Print the folder path
        LPWSTR wszFolderPath;
        hr = pShellItem2->GetString(PKEY_ParsingPath, &wszFolderPath);
        if (SUCCEEDED(hr))
        {
            _putws(wszFolderPath);
        }
        CoTaskMemFree(wszFolderPath);

        // Clean up
        pShellItem2->Release();
    }

    pShellItemArray->Release();
}
  C# ListFoldersInShellLibrary

using (ShellLibrary library = ShellLibrary.Load(libraryName, false))
{
    /**//
    // List all folders in the library.
    //

    foreach (ShellFolder folder in library)
    {
        Console.WriteLine(folder);
    }
}

  删除一个Shell Library

  C++ DeleteShellLibrary

/**//*!
* Delete the shell library under the user's Libraries folder according to the
* specified library name.
*
* \param pwszLibraryName
* The name of the shell library to be deleted.
*/
BOOL DeleteShellLibrary(LPWSTR pwszLibraryName)
{
    /**//
    // Get the shell item that represents the library and its full path.
    //

    IShellItem2* pShellItem = GetShellLibraryItem(pwszLibraryName);

    // Get the file-system full path of the shell item
    LPWSTR wszLibraryFullPath;
    pShellItem->GetString(PKEY_ParsingPath, &wszLibraryFullPath);


    /**//
    // Delete file with the library file-system based full path.
    //

    BOOL bSuccess = DeleteFileW(wszLibraryFullPath);

    // Clean up
    CoTaskMemFree(wszLibraryFullPath);
    if (pShellItem != NULL)
        pShellItem->Release();

    return bSuccess;
}


  C# DeleteShellLibrary

/**//
// Delete the shell library.
//

string librariesPath = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData),
    ShellLibrary.LibrariesKnownFolder.RelativePath); 
 

string libraryPath = Path.Combine(librariesPath, libraryName);
string libraryFullPath = Path.ChangeExtension(libraryPath, "library-ms");

File.Delete(libraryFullPath); 
  更多关于Windows 7 Shell Library的操作请参见CppWin7ShellLibrary, CSWin7ShellLibrary和VBWin7ShellLibrary示例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Shell 编程是指在Windows操作系统中使用命令行界面(也称为Shell)来编写和执行脚本或命令的过程。这种编程技术可以用于自动化和简化一些常见和重复的任务,提高工作效率。 与其他编程语言相比,Windows Shell 编程具有其独特的优势。首先,它是内置在Windows操作系统中的,因此不需要安装额外的工具或软件包。其次,它与操作系统的集成非常紧密,因此可以轻松地访问和管理系统资源,如文件、文件夹、注册表等。此外,由于命令行界面的交互性较低,Shell 脚本往往比其他脚本语言编写的脚本更加简洁和高效。 为了学习和掌握Windows Shell 编程,可以参考一些相关的资源,其中包括一些提供了Windows Shell 编程的PDF文档。 这些PDF文档通常包含了Windows Shell 脚本编程的基础知识、语法规则、常用命令、示例代码等内容。通过阅读和学习这些文档,你可以了解如何使用Windows Shell 编程来完成各种任务,如文件操作、进程管理、注册表操作等。 此外,网络上还有许多免费的教程和指南,可以帮助初学者理解和应用Windows Shell 编程。通过结合实践和实际应用,你可以逐渐掌握并应用这一技术来解决实际工作中的问题。 总之,Windows Shell 编程是一项非常有用的技能,能够帮助我们提高工作效率和自动化一些常见任务。通过学习相关的PDF文档和资源,我们可以快速入门并掌握这项技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值