FileSystemクラス

ファイルやディレクトリのための有用な機能が提供されます。

このクラスのメソッドは複数のファイルを一括して処理できないため、その必要があるときはWin32のSHFileOperation()やIFileOperationインターフェイスを用います。

// ダイアログが表示され、1つめのファイルがコピーされる
FileSystem.CopyFile(@"C:\a.txt", @"C:\a1.txt", UIOption.AllDialogs);

// ダイアログが再び表示され、2つめのファイルがコピーされる
FileSystem.CopyFile(@"C:\a.txt", @"C:\a2.txt", UIOption.AllDialogs);

SHFileOperation()関数

C#から呼び出す方法は、SHFileOperation() - NativeMethods.csにコードがあります。

IFileOperationインターフェイス

これをC#から呼び出す方法は.NET Matters: IFileOperation in Windows Vista | Microsoft Learnに解説があり、ソースコードはNetMatters2007_12.exeからダウンロードできます。

  • ファイルを同一のフォルダにコピーすると転送速度が表示されず、進捗も不適切に表示されます。
  • ファイルの削除時にごみ箱に移動されるかは、既定ではエクスプローラの設定に依存します。ただしUSBメモリなどのリムーバブルデバイスではごみ箱に移動されないため、つねに完全に削除されます。
  • 確認ダイアログが表示されるかどうかは、既定ではエクスプローラの設定に依存します。

SetOperationFlags()

操作のパラメータを指定できます。これを指定しなかった場合はFOF_ALLOWUNDO | FOF_NOCONFIRMMKDIRとなります。

定数 内容
FOF_ALLOWUNDO 0x0040 Preserve undo information, if possible.
FOF_FILESONLY 0x0080 Perform the operation only on files (not on folders) if a wildcard file name (.) is specified.
FOF_NOCONFIRMATION 0x0010 Respond with Yes to All for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR 0x0200 Do not confirm the creation of a new folder if the operation requires one to be created.
FOF_NO_CONNECTED_ELEMENTS 0x2000 Do not move connected items as a group. Only move the specified files.
FOF_NOCOPYSECURITYATTRIBS 0x0800 Do not copy the security attributes of the item.
FOF_NOERRORUI 0x0400 Do not display a message to the user if an error occurs. If this flag is set without FOFX_EARLYFAILURE, any error is treated as if the user had chosen Ignore or Continue in a dialog box. It halts the current action, sets a flag to indicate that an action was aborted, and proceeds with the rest of the operation.
FOF_NORECURSION 0x1000 Only operate in the local folder. Do not operate recursively into subdirectories.
FOF_RENAMEONCOLLISION 0x0008 Give the item being operated on a new name in a move, copy, or rename operation if an item with the target name already exists.
FOF_SILENT 0x0004 Do not display a progress dialog box.
FOF_WANTNUKEWARNING 0x4000 Send a warning if a file or folder is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
FOFX_ADDUNDORECORD 0x20000000 Introduced in Windows 8. The file operation was user-invoked and should be placed on the undo stack. This flag is preferred to FOF_ALLOWUNDO.
FOFX_NOSKIPJUNCTIONS 0x00010000 Walk into Shell namespace junctions. By default, junctions are not entered. For more information on junctions, see Specifying a Namespace Extension's Location.
FOFX_PREFERHARDLINK 0x00020000 If possible, create a hard link rather than a new instance of the file in the destination.
FOFX_SHOWELEVATIONPROMPT 0x00040000 If an operation requires elevated rights and the FOF_NOERRORUI flag is set to disable error UI, display a UAC UI prompt nonetheless.
FOFX_EARLYFAILURE 0x00100000 If FOFX_EARLYFAILURE is set together with FOF_NOERRORUI, the entire set of operations is stopped upon encountering any error in any operation. This flag is valid only when FOF_NOERRORUI is set.
FOFX_PRESERVEFILEEXTENSIONS 0x00200000 Rename collisions in such a way as to preserve file name extensions. This flag is valid only when FOF_RENAMEONCOLLISION is also set.
FOFX_KEEPNEWERFILE 0x00400000 Keep the newer file or folder, based on the Date Modified property, if a collision occurs. This is done automatically with no prompt UI presented to the user.
FOFX_NOCOPYHOOKS 0x00800000 Do not use copy hooks.
FOFX_NOMINIMIZEBOX 0x01000000 Do not allow the progress dialog to be minimized.
FOFX_MOVEACLSACROSSVOLUMES 0x02000000 Copy the security attributes of the source item to the destination item when performing a cross-volume move operation. Without this flag, the destination item receives the security attributes of its new folder.
FOFX_DONTDISPLAYSOURCEPATH 0x04000000 Do not display the path of the source item in the progress dialog.
FOFX_DONTDISPLAYDESTPATH 0x08000000 Do not display the path of the destination item in the progress dialog.
FOFX_RECYCLEONDELETE 0x00080000 Introduced in Windows 8. When a file is deleted, send it to the Recycle Bin rather than permanently deleting it.
FOFX_REQUIREELEVATION 0x10000000 Introduced in Windows Vista SP1. The user expects a requirement for rights elevation, so do not display a dialog box asking for a confirmation of the elevation.
FOFX_COPYASDOWNLOAD 0x40000000 Introduced in Windows 7. Display a Downloading instead of Copying message in the progress dialog.
FOFX_DONTDISPLAYLOCATIONS 0x80000000 Introduced in Windows 7. Do not display the location line in the progress dialog.
IFileOperation::SetOperationFlags (shobjidl_core.h) - Win32 apps | Microsoft Learn
Microsoft Learnから検索