Windows本机文件复制多个文件对话框

huangapple go评论80阅读模式
英文:

windows native copy multple file dialoge

问题

如何复制或移动多个文件并显示各自的本机文件对话框?就像这样:

虽然我可以使用以下代码复制多个文件:

但它为每个文件分别显示,就像这样:

我知道已经有FileSystem.CopyDirectory(...)。但我不想复制整个目录。我只想复制目录中的一些文件,而不需要多个提示或单独的进度。

我相信这是可能的,因为我在TeamViewer和AnyDesk软件中看到过。

英文:

How to copy or move MULTIPLE files and show respective native file dialog ? like this:
Windows本机文件复制多个文件对话框

Although I can copy multiple files using this code:

using Microsoft.VisualBasic.FileIO;
...
FileSystem.CopyFile("1.txt", "2.txt");
FileSystem.CopyFile("3.txt", "4.txt");
...

But it shows separately for each of files like:
Windows本机文件复制多个文件对话框

I know there's already FileSystem.CopyDirectory(...). But I don't want to copy whole directory. I just want some files from directory to be copied without multiple prompts or separate progress.

I believe its possible. Because I seen that in team viewer and anydesk software.

答案1

得分: 1

扩展我之前的评论,我们需要通过COM调用IFileOperation,因此会涉及一些挂钩和封送。供参考,2007年的MSDN存档文章详细解释了整个过程:Steven Toub的"Windows Vista中的IFileOperation"。

1PictureManager的作者已经将它们封装成一个相当干净的实现。他们慷慨地将整个项目许可为CC0。这将使我们不必自己收集所有Shell P/Invoke代码。

  1. 从PictureManager存储库中获取ShellStuff文件夹

  2. 删除FileInformation.csPicFileOperationProgressSink.cs。它们是特定于该应用程序的,我们不需要它们。

  3. 编辑您的.csproj文件,并添加以下内容:

<Project ...>
  ...
  <!-- 添加整个ItemGroup: -->
  <ItemGroup>
    <COMReference Include="Shell32">
      <WrapperTool>tlbimp</WrapperTool>
      <VersionMinor>0</VersionMinor>
      <VersionMajor>1</VersionMajor>
      <Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
      <EmbedInteropTypes>true</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>
  <!-- 结束添加 -->
</Project>
  1. 现在在您的代码中使用它,如下所示:
using var fo = new FileOperation();

// 有很多标志,所有都有良好的注释。
fo.SetOperationFlags(FileOperationFlags.FOF_NOCONFIRMMKDIR);

// 定义要复制的文件:
fo.CopyItem(@"C:\somewhere\old1.zip", @"C:\elsewhere\", "new1.zip");
fo.CopyItem(@"C:\somewhere\old2.zip", @"C:\elsewhere\", "new2.zip");
// ...等等

// 一次执行它们全部:
fo.PerformOperations();

您应该看到本地对话框执行复制操作,包括本地确认对话框,如果指定名称的文件已经存在于目标位置。

英文:

Expanding on my earlier comment, we will need to invoke IFileOperation through COM - so it's going to be a little dirty with the hooking and marshalling.

For reference, there is an archived MSDN article from 2007 that explains the whole thing step-by-step: "IFileOperation in Windows Vista" by Steven Toub.

The author of PictureManager has nicely wrapped them in a pretty clean implementation. They have kindly licensed the whole project as CC0. This'll save us from having to collect all the Shell P/Invoke code ourselves.

  1. Grab the ShellStuff folder from the PictureManager repo.

  2. Delete the FileInformation.cs and PicFileOperationProgressSink.cs.
    They're specific to the app and we won't need them.

  3. Edit your .csproj file, and add the following:

&lt;Project ...&gt;
  ...
  &lt;!-- Add this whole ItemGroup: --&gt;
  &lt;ItemGroup&gt;
    &lt;COMReference Include=&quot;Shell32&quot;&gt;
      &lt;WrapperTool&gt;tlbimp&lt;/WrapperTool&gt;
      &lt;VersionMinor&gt;0&lt;/VersionMinor&gt;
      &lt;VersionMajor&gt;1&lt;/VersionMajor&gt;
      &lt;Guid&gt;50a7e9b0-70ef-11d1-b75a-00a0c90564fe&lt;/Guid&gt;
      &lt;Lcid&gt;0&lt;/Lcid&gt;
      &lt;Isolated&gt;false&lt;/Isolated&gt;
      &lt;EmbedInteropTypes&gt;true&lt;/EmbedInteropTypes&gt;
    &lt;/COMReference&gt;
  &lt;/ItemGroup&gt;
  &lt;!-- End add --&gt;
&lt;/Project&gt;
  1. Now use it in your code like so:
using var fo = new FileOperation();

// There&#39;s a whole bunch of flags, all of them well commented.
fo.SetOperationFlags(FileOperationFlags.FOF_NOCONFIRMMKDIR);

// Define the files you&#39;d like to copy:
fo.CopyItem(@&quot;C:\somewhere\old1.zip&quot;, @&quot;C:\elsewhere\&quot;, @&quot;new1.zip&quot;);
fo.CopyItem(@&quot;C:\somewhere\old2.zip&quot;, @&quot;C:\elsewhere\&quot;, @&quot;new2.zip&quot;);
// ...etc

// Do them all in one go:
fo.PerformOperations();

You should see the native dialog performing the copy operations. Including the native confirmation dialog if files with the specified names already exist in the destination.

huangapple
  • 本文由 发表于 2023年2月19日 17:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定