<legend id='2s7jC'><style id='2s7jC'><dir id='2s7jC'><q id='2s7jC'></q></dir></style></legend><tfoot id='2s7jC'></tfoot>
    • <bdo id='2s7jC'></bdo><ul id='2s7jC'></ul>

        <small id='2s7jC'></small><noframes id='2s7jC'>

        <i id='2s7jC'><tr id='2s7jC'><dt id='2s7jC'><q id='2s7jC'><span id='2s7jC'><b id='2s7jC'><form id='2s7jC'><ins id='2s7jC'></ins><ul id='2s7jC'></ul><sub id='2s7jC'></sub></form><legend id='2s7jC'></legend><bdo id='2s7jC'><pre id='2s7jC'><center id='2s7jC'></center></pre></bdo></b><th id='2s7jC'></th></span></q></dt></tr></i><div id='2s7jC'><tfoot id='2s7jC'></tfoot><dl id='2s7jC'><fieldset id='2s7jC'></fieldset></dl></div>
      1. 使用 IFileOperation 在复制期间创建目录

        时间:2023-07-26
        <tfoot id='xAbVT'></tfoot>

            <bdo id='xAbVT'></bdo><ul id='xAbVT'></ul>
          • <i id='xAbVT'><tr id='xAbVT'><dt id='xAbVT'><q id='xAbVT'><span id='xAbVT'><b id='xAbVT'><form id='xAbVT'><ins id='xAbVT'></ins><ul id='xAbVT'></ul><sub id='xAbVT'></sub></form><legend id='xAbVT'></legend><bdo id='xAbVT'><pre id='xAbVT'><center id='xAbVT'></center></pre></bdo></b><th id='xAbVT'></th></span></q></dt></tr></i><div id='xAbVT'><tfoot id='xAbVT'></tfoot><dl id='xAbVT'><fieldset id='xAbVT'></fieldset></dl></div>

            <legend id='xAbVT'><style id='xAbVT'><dir id='xAbVT'><q id='xAbVT'></q></dir></style></legend>
            1. <small id='xAbVT'></small><noframes id='xAbVT'>

                  <tbody id='xAbVT'></tbody>
                • 本文介绍了使用 IFileOperation 在复制期间创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Stephen Toub 的 C# 的 IFileOperation 包装器(link),到目前为止一直运行良好.现在我正在尝试复制以从网络位置收集文件,每个网络位置到它自己的子目录中.

                  Using Stephen Toub's IFileOperation wrapper for C# (link), which has been working well until now. Now I am trying to do a copy to gather files from network locations, each network location into its own subdirectory.

                  \FOOdataC:gatherFoo_data
                  \BARmanagecurrentC:gatherarmanage

                  等等.问题出在 FileOperation.CopyItem 中.一定是因为目标目录还不存在——IFileOperation 会在复制过程中创建它,对吧?我使用了 another question 中的技术并更改了 Toub 的FileOperation.CreateShellItem 到这个:

                  And so on. The problem is in FileOperation.CopyItem. It must be because the destination directory doesn't exist yet—IFileOperation will create it during the copy, right? I used the technique from another question and changed Toub's FileOperation.CreateShellItem to this:

                  private static ComReleaser<IShellItem> CreateShellItem( string path )
                  {
                      try
                      {
                          return new ComReleaser<IShellItem>( (IShellItem)SHCreateItemFromParsingName( path, null, ref _shellItemGuid ) );
                      }
                      catch ( FileNotFoundException )
                      {
                          IntPtr pidl = SHSimpleIDListFromPath( path );
                          IShellItem isi = (IShellItem)SHCreateItemFromIDList( pidl, ref _shellItemGuid );
                          Marshal.FreeCoTaskMem( pidl );
                          System.Diagnostics.Debug.WriteLine( "Shell item: " + isi.GetDisplayName( SIGDN.DesktopAbsoluteParsing ) );
                          return new ComReleaser<IShellItem>( isi );
                      }
                  }
                  

                  我将 Debug.WriteLine 卡在其中以检查它是否正常工作,并且似乎工作正常;它会写回路径.

                  I stuck the Debug.WriteLine in there to check that it's working, and it seems to be working fine; it writes the path back out.

                  但是 IFileOperation.CopyItem 抛出 ArgumentException,我不知道为什么.我没有正确执行IShellItem for a nonexistent file"吗?我怀疑我需要在其中获取 SFGAO_FOLDER,因为我正在尝试为不存在的 directory 创建一个 IShellItem,而不是文件,但是如何?

                  But IFileOperation.CopyItem throws an ArgumentException, and I can't figure out why. Am I not doing the "IShellItem for a nonexistent file" correctly? I suspect I need to get SFGAO_FOLDER in there, since I am trying to create an IShellItem for a nonexistent directory, not file, but how?

                  推荐答案

                  在这个周末(多么疯狂的派对周末)进行了大量谷歌搜索和编码实验后,我找到了解决方案.

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

                  1. 编写一个实现IFileSystemBindData的COM兼容类.
                  2. 分配一个 WIN32_FIND_DATA 结构并设置它的 dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY.(其他成员在这里无关紧要,据我经过一些测试后得知.)
                  3. 创建您的 IFileSystemBindData 类的实例.
                  4. 将您的 WIN32_FIND_DATA 对象传递给 IFileSystemBindData::SetFindData.
                  5. 创建一个 IBindCtx 的实例.
                  6. 使用 STR_FILE_SYS_BIND_DATA 的参数调用它的 IBindCtx::RegisterObjectParam (#defined as L"File System Bind Data") 和您的 IFileSystemBindData 对象.
                  7. 使用要创建的目录的路径名和您的 IBindCtx 对象调用 SHCreateItemFromParsingName.
                  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.

                  它有效.binding-context 对象告诉 SHCreateItemFromParsingName 不要查询文件系统,而只使用它给出的内容.我让 IFileOperation 将文件复制到新的且命名巧妙的目录 D:SomePathThatDidNotPreviousExist 中,它创建了所有沿途有七个目录.据推测,同样的技术可用于为任何不存在的文件系统对象创建 IShellItem,具体取决于您在 dwFileAttributes 中输入的内容.

                  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:SomePathThatDidNotPreviouslyExist, 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.

                  但这很丑.我希望有更好的方法.我的编码实验是在 C++ 中进行的.现在编写 COM 互操作的东西以在 C# 中再次执行此操作.或者,放弃项目并使用 C++ 重新开始会更容易.

                  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++.

                  这篇关于使用 IFileOperation 在复制期间创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 C# 中枚举 COM 对象的成员? 下一篇:为什么需要在 c# 中使用 DispId 注释?

                  相关文章

                • <i id='ckTzG'><tr id='ckTzG'><dt id='ckTzG'><q id='ckTzG'><span id='ckTzG'><b id='ckTzG'><form id='ckTzG'><ins id='ckTzG'></ins><ul id='ckTzG'></ul><sub id='ckTzG'></sub></form><legend id='ckTzG'></legend><bdo id='ckTzG'><pre id='ckTzG'><center id='ckTzG'></center></pre></bdo></b><th id='ckTzG'></th></span></q></dt></tr></i><div id='ckTzG'><tfoot id='ckTzG'></tfoot><dl id='ckTzG'><fieldset id='ckTzG'></fieldset></dl></div>

                  <tfoot id='ckTzG'></tfoot>

                    1. <small id='ckTzG'></small><noframes id='ckTzG'>

                        <bdo id='ckTzG'></bdo><ul id='ckTzG'></ul>
                    2. <legend id='ckTzG'><style id='ckTzG'><dir id='ckTzG'><q id='ckTzG'></q></dir></style></legend>