MSDN Magazine - December 2007 - (Page 117) IFileOperation in Windows Vista STEPHEN TOUB I have a bunch of file operations I’d like to execute as a batch, and I’d like to get the standard Windows® progress UI for my operations. I know I could use classes from the System.IO namespace to do all of the file operations one after the other, but then I would need to create my own progress UI, which is way more work than I wish to undertake. I noticed that Windows Vista® includes a new IFileOperations interface, but none of the samples I’ve seen demonstrate how to use this from managed code. How can it be done? Q Windows Vista does indeed include a new copy engine that supports exactly what you’re looking to do. However, it’s possible that previously existing functionality may meet your needs. For example, if you want to copy, move, rename, or delete an individual file or directory, you can take advantage of SHFileOperation (exposed from shell32.dll), which is already wrapped by the Visual Basic® runtime. If you’re using Visual Basic 2005, you can simply use functionality from the My namespace, for example: My.Computer.FileSystem.CopyDirectory( sourcePath, destinationPath, UIOption.AllDialogs) A Figure 1 Windows Vista Progress Dialog in-depth look, see shellrevealed.com/blogs/shellblog/archive/2007/04/16/ IFileOperation-_1320_-Part-1_3A00_-Introduction.aspx. Accomplishing the same thing in C# involves only a little more work, adding a reference to Microsoft.VisualBasic.dll (from the Microsoft® .NET Framework installation directory) and using code such as the following: using Microsoft.VisualBasic.FileIO; FileSystem.CopyDirectory( sourcePath, destinationPath, UIOption.AllDialogs); When run, this will result in the same progress UI you’d see if you were doing the same file operations from Windows Explorer. In fact, when running on Windows Vista, you automatically get the new Window Vista progress UI, as shown in Figure 1. However, SHFileOperation and the wrappers provided by .NET are limited to operating on one file system object (which could be a directory) and one operation (copy, move, rename, or delete) per call; you can’t mix and match or operate on disparate files within the same operation. That’s where IFileOperation comes in. IFileOperation is the successor to SHFileOperation and provides a wealth of new functionality, including the ability to batch a bunch of operations, which can be a mix of copies, renames, moves, deletes, and even the creation of new items. It can also operate on any set of files, not just those in the same directory. These are just a few of the cool features provided through IFileOperation; for a more Unfortunately, as you point out, at the time of this writing there are currently no managed wrappers provided by Microsoft to expose this functionality from managed code. As such, I’ve built one. You can download the code from the MSDN® Magazine Web site, and I’ll spend the rest of this answer walking through how to use it and how it was implemented. Note that my wrapper is not meant to expose all of IFileOperation’s functionality, nor is it meant to completely abstract away all of the underlying bits and bytes and provide a perfect .NET wrapper that follows all of the standard .NET design guidelines; it’s simply meant to easily expose IFileOperation’s functionality so you can get up and running with the basics in your Windows Vista-targeting applications today. Let’s begin with the IFileOperation interface itself, for which I’ve shown my managed definition in Figure 2 (this is based on the IDL from the ShObjIdl.idl file in the Windows SDK). The interface is designed such that you schedule with it all of the operations you want performed, and then when you’re ready, you tell it to execute them all. At that point, it’ll begin execution, and if it senses that the operations may take a noticeable amount of time, it’ll present the standard Windows Vista progress dialog, complete with descriptions about everything it is doing. To schedule actions, you use the IFileOperation methods CopyItem, MoveItem, RenameItem, DeleteItem, and NewItem. (The interface also exposes plural versions of those methods, but since their behavior can be simulated as necessary with the singular versions and because they’re more difficult to work with from managed code, I won’t be discussing them further.) Each of these methods merely expresses your intent for the operation to happen, but the actions aren’t actually december2007 117 http://shellrevealed.com/blogs/shellblog/archive/2007/04/16/IFileOperation-_1320_-Part-1_3AO0_Introduction.aspx http://shellrevealed.com/blogs/shellblog/archive/2007/04/16/IFileOperation-_1320_-Part-1_3AO0_Introduction.aspx
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.