英文:
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:
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:
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"。
1。PictureManager的作者已经将它们封装成一个相当干净的实现。他们慷慨地将整个项目许可为CC0。这将使我们不必自己收集所有Shell P/Invoke代码。
-
从PictureManager存储库中获取
ShellStuff
文件夹。 -
删除
FileInformation.cs
和PicFileOperationProgressSink.cs
。它们是特定于该应用程序的,我们不需要它们。 -
编辑您的
.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>
- 现在在您的代码中使用它,如下所示:
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.
-
Grab the
ShellStuff
folder from the PictureManager repo. -
Delete the
FileInformation.cs
andPicFileOperationProgressSink.cs
.
They're specific to the app and we won't need them. -
Edit your
.csproj
file, and add the following:
<Project ...>
...
<!-- Add this whole 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>
<!-- End add -->
</Project>
- Now use it in your code like so:
using var fo = new FileOperation();
// There's a whole bunch of flags, all of them well commented.
fo.SetOperationFlags(FileOperationFlags.FOF_NOCONFIRMMKDIR);
// Define the files you'd like to copy:
fo.CopyItem(@"C:\somewhere\old1.zip", @"C:\elsewhere\", @"new1.zip");
fo.CopyItem(@"C:\somewhere\old2.zip", @"C:\elsewhere\", @"new2.zip");
// ...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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论