C# IFileOperation 如何获取所有操作的状态和错误信息
C# IFileOperation How to get status and error information for all operations
我想在我的 .NET C# 应用程序中使用 IFileOperation。
我找到这篇文章 http://blogs.msdn.com/b/msdnmagazine/archive/2007/12/12/6738569.aspx (source code is available here 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 class 中获取所有这些信息,因此我可以像 fileOp.ResultData[].[=13= 一样访问它们]
有人可以帮我吗?
我想通了。
这是我的 FileOperationProgressSink class,它将源文件路径和新创建的文件路径添加到 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[=11=]3.jpg", @"d:\!test\aaa", "003.jpg");
fo.PerformOperations();
}
var fileOperationResult = (Dictionary<string, string>) Application.Current.Properties["FileOperationResult"];
}
然后我可以比较 input/output 个文件路径并确定发生了什么。
我想在我的 .NET C# 应用程序中使用 IFileOperation。
我找到这篇文章 http://blogs.msdn.com/b/msdnmagazine/archive/2007/12/12/6738569.aspx (source code is available here 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 class 中获取所有这些信息,因此我可以像 fileOp.ResultData[].[=13= 一样访问它们]
有人可以帮我吗?
我想通了。
这是我的 FileOperationProgressSink class,它将源文件路径和新创建的文件路径添加到 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[=11=]3.jpg", @"d:\!test\aaa", "003.jpg");
fo.PerformOperations();
}
var fileOperationResult = (Dictionary<string, string>) Application.Current.Properties["FileOperationResult"];
}
然后我可以比较 input/output 个文件路径并确定发生了什么。