什么是“Shell命名空间”创建新文件夹的方法?

时间:2009-11-20 21:33:57

标签: c++ windows com windows-shell

显然,这对于win32 api - CreateDirectory()来说是微不足道的。但是我正在尝试托管一个IShellView,并希望以最以shell为导向的方式。我原本以为会有一个createobject或createfolder或来自IShellFolder的一些。但IShellView和IShellFolder,甚至IFolderView似乎都没有这样的东西。

是否有Shell编程方式来创建新文件夹?或者我是否需要使用路径名创建一个文件夹,这是一种老式的方式?

如果我必须通过CreateDirectory()来做,那么我的下一个问题可能是:关于如何让IShellView / IFolderView实际看到这个新对象并将其显示给用户的任何想法?

动机:创建我自己的文件对话框替换,我想提供标准XP风格文件对话框的“新文件夹”工具栏图标功能。

编辑:我继续使用CreateDirectory创建了一些基本可行的东西。但是,我仍然希望有更好的方法来做到这一点,但是你可以看到它是如何工作的,并提供更好的想法来更好地解决这个问题:

    PidlUtils::Pidl pidl(m_folder);
    CFilename folderName(GetDisplayNameOf(pidl), "New Folder");
    for (int i = 2; folderName.Exists(); ++i)
        folderName.SetFullName(FString("New Folder (%d)", i));
    if (!CPathname::Create(folderName, false))
        throw CContextException("Unable to create a new folder here: ");

    // get the PIDL for the newly created folder
    PidlUtils::Pidl pidlNew;
#ifdef UNICODE
    const wchar_t * wszName = folderName.c_str();
#else
    wchar_t wszName[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, folderName.GetFullName(), -1, wszName, MAX_PATH);
#endif
    m_hresult = m_folder->ParseDisplayName(NULL, NULL, wszName, NULL, pidlNew, NULL);
    if (FAILED(m_hresult))
        throw CLabeledException(FString("Unable to get the PIDL for the new folder: 0x%X", m_hresult));

    // upgrade our interface so we can select & rename it
    CComQIPtr<IShellView2> sv2(m_shell_view);
    if (!sv2)
        throw CLabeledException("Unable to obtain the IShellView2 we need to rename the newly created folder.");

    // force it to see thew new folder
    sv2->Refresh();

    // select the new folder, and begin the rename process
    m_hresult = sv2->SelectAndPositionItem(pidlNew, SVSI_EDIT|SVSI_DESELECTOTHERS|SVSI_ENSUREVISIBLE|SVSI_POSITIONITEM, NULL);
    if (FAILED(m_hresult))
        throw CLabeledException(FString("Unable to select and position the new folder item: 0x%X", m_hresult));

3 个答案:

答案 0 :(得分:4)

是的,您可以get IContextMenu查找子菜单,但为什么还要麻烦,只需在调用CreateDirectory后调用SHChangeNotify

答案 1 :(得分:1)

Win32 API函数CreateDirectory似乎是“正确的方法”。至少,我找不到更好的东西 - 这里的答案并没有真正阐明更好的方法。

答案 2 :(得分:0)

Shell文件夹通常实现IStorage接口,因此非常简单。例如,以下内容在桌面上创建名为“abcd”的文件夹:

  CComPtr<IShellFolder> pDesktop;
  HRESULT hr = SHGetDesktopFolder(&pDesktop);
  if (FAILED(hr)) return;
  CComQIPtr<IStorage> pStorage(pDesktop);
  if (!pStorage) return;
  CComPtr<IStorage> dummy;
  hr = pStorage->CreateStorage(L"abcd", STGM_FAILIFTHERE, 0, 0, &dummy);