【问题标题】:C# IFileOperation How to get status and error information for all operationsC# IFileOperation 如何获取所有操作的状态和错误信息
【发布时间】:2016-03-28 20:23:36
【问题描述】:

我想在我的 .NET C# 应用程序中使用 IFileOperation。

我找到了这篇文章http://blogs.msdn.com/b/msdnmagazine/archive/2007/12/12/6738569.aspx(源代码在这里http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/NetMatters2007_12.exe)。 到目前为止它运行良好,但我想获取所有操作的状态和错误信息。

有一行代码:

if (_callbackSink != null) _sinkCookie = _fileOperation.Advise(_callbackSink);

这应该允许我访问这些信息,但我不知道如何使用它。

这就是我的称呼,我想在 fileOp.PerformOperations(); 之后获得一些带有结果的操作列表;类似的东西:

File/Folder name | Action | Result 
d:\test\      | Copy | OK
d:\test\a.jpg | Copy | OK
d:\test\b.jpg | Copy | CALCELED

using (FileOperation fileOp = new FileOperation(new FileOperationProgressSink(), this)) {
  fileOp.CopyItem(source, destination, name);
  fileOp.PerformOperations();
}

我知道我可以在 FileOperationProgressSink.PostCopyItem 中获取这些信息,但我需要它们都在 FileOperation 类中,所以我可以像 fileOp.ResultData[] 一样访问它们。

有人可以帮我吗?

【问题讨论】:

    标签: c# file winapi com dllimport


    【解决方案1】:

    我想通了。

    这是我的 FileOperationProgressSink 类,它将源文件路径和新创建的文件路径添加到 Application.Current.Properties 中的字典。

    using System.Collections.Generic;
    using System.Windows;
    using PictureManager.ShellStuff.Interfaces;
    
    namespace PictureManager.ShellStuff {
      public class PicFileOperationProgressSink: FileOperationProgressSink {
        public override void PostCopyItem(uint dwFlags, IShellItem psiItem, IShellItem psiDestinationFolder, string pszNewName, uint hrCopy, IShellItem psiNewlyCreated) {
          if (hrCopy != 0) return;
          ((Dictionary<string, string>)Application.Current.Properties["FileOperationResult"]).Add(
            psiItem.GetDisplayName(SIGDN.SIGDN_FILESYSPATH), 
            psiNewlyCreated.GetDisplayName(SIGDN.SIGDN_FILESYSPATH));
        }
      }
    }
    

    这是 FileOperation 的用法

    private void CmdTestButton(object sender, ExecutedRoutedEventArgs e) {
      Application.Current.Properties["FileOperationResult"] = new Dictionary<string, string>();
      using (FileOperation fo = new FileOperation(new PicFileOperationProgressSink())) {
        fo.CopyItem(@"d:\!test\003.jpg", @"d:\!test\aaa", "003.jpg");
        fo.PerformOperations();
      }
      var fileOperationResult = (Dictionary<string, string>) Application.Current.Properties["FileOperationResult"];
    }
    

    然后我可以比较输入/输出文件路径并确定发生了什么。

    【讨论】:

      猜你喜欢
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      相关资源
      最近更新 更多