Creating directories during a copy using IFileOperation

心已入冬 提交于 2019-12-03 07:54:44

After a lot of Googling and coding experimentation this weekend (what a wild party weekend), I found a solution.

  1. Write a COM-compatible class that implements IFileSystemBindData.
  2. Allocate a WIN32_FIND_DATA structure and set its dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY. (The other members are irrelevant here, as far as I can tell after some testing.)
  3. Create an instance of your IFileSystemBindData class.
  4. Pass your WIN32_FIND_DATA object to IFileSystemBindData::SetFindData.
  5. Create an instance of IBindCtx.
  6. Call its IBindCtx::RegisterObjectParam with arguments of STR_FILE_SYS_BIND_DATA (#defined as L"File System Bind Data") and your IFileSystemBindData object.
  7. Call SHCreateItemFromParsingName with the pathname of the directory to create, and your IBindCtx object.

It works. The binding-context object tells SHCreateItemFromParsingName not to query the filesystem, and just use what it's given. I got IFileOperation to copy a file into the new and cleverly-named directory D:\Some\Path\That\Did\Not\Previously\Exist, and it created all seven directories along the way. Presumably this same technique could be used to create an IShellItem for any non-existent filesystem object, depending on what you put in dwFileAttributes.

But it's ugly. I hope there's a better way. My coding experimentation was in C++; now to write the COM interop stuff to do it again in C#. Or maybe it will be easier to throw out the project and start over in C++.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!