中の技術日誌
 ホーム / 上へ

わんくまライブラリ Wankuma.Interop.InteropSHFileOperationクラス Version1

2005/07/17

この文書はVisual Studio 2003(.NET1.1)をベースに記述されています。それ以降のバージョンや、あなたが読んでいる時点では変更されている可能性があります。
またバージョンアップされている場合にはなんらかかの不具合を含んでいる可能性があります。

ドキュメントへ
Wankuma.Interop.InteropSHFileOperation1d.htm

ソースファイル直接ダウンロードへ
Wankuma.Interop.InteropSHFileOperation1c.txt

利用規約へ
../kiyaku.htm

using System;
using System.Runtime.InteropServices;

namespace Wankuma.Interop
{

  /// <summary>
  /// SHFileOperationの処理クラス
  /// </summary>
  public class InteropSHFileOperation
  {
    /// <summary>
    /// FO_Func
    /// </summary>
    public enum FO_Func : uint
    {
      /// <summary>
      /// Move
      /// </summary>
      FO_MOVE = 0x0001,
      /// <summary>
      /// Copy
      /// </summary>
      FO_COPY = 0x0002,
      /// <summary>
      /// Delete
      /// </summary>
      FO_DELETE = 0x0003,
      /// <summary>
      /// Rename
      /// </summary>
      FO_RENAME = 0x0004,
    }

    /// <summary>
    /// SHFILEOPSTRUCT
    /// </summary>
    struct SHFILEOPSTRUCT
    {
      public IntPtr hwnd;
      public FO_Func wFunc;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string pFrom;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string pTo;
      public ushort fFlags;
      public bool fAnyOperationsAborted;
      public IntPtr hNameMappings;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string lpszProgressTitle;

    }
    
    [DllImport("shell32.dll",  CharSet = CharSet.Unicode)]
    static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp);


    private SHFILEOPSTRUCT _ShFile;
    /// <summary>
    /// FILEOP_FLAGS
    /// </summary>
    public FILEOP_FLAGS fFlags;

    /// <summary>
    /// HWND
    /// </summary>
    public IntPtr hwnd
    {
      set
      {
        this._ShFile.hwnd = value;
      }
    }
    /// <summary>
    /// 処理モード
    /// </summary>
    public FO_Func wFunc
    {
      set
      {
        this._ShFile.wFunc = value;
      }
    }

    /// <summary>
    /// 処理From
    /// </summary>
    public string pFrom
    {
      set
      {
        this._ShFile.pFrom = value + '\0' + '\0';
      }
    }

    /// <summary>
    /// 処理To
    /// </summary>
    public string pTo
    {
      set
      {
        this._ShFile.pTo = value + '\0' + '\0';
      }
    }

    /// <summary>
    /// fAnyOperationsAborted
    /// </summary>
    public bool fAnyOperationsAborted
    {
      set
      {
        this._ShFile.fAnyOperationsAborted = value;
      }
    }
    /// <summary>
    /// hNameMappings
    /// </summary>
    public IntPtr hNameMappings
    {
      set
      {
        this._ShFile.hNameMappings = value;
      }
    }
    /// <summary>
    /// lpszProgressTitle
    /// </summary>
    public string lpszProgressTitle
    {
      set
      {
        this._ShFile.lpszProgressTitle = value + '\0';
      }
    }


    /// <summary>
    /// InteropSHFileOperation
    /// </summary>
    public InteropSHFileOperation()
    {
      
      this.fFlags = new FILEOP_FLAGS();
      this._ShFile = new SHFILEOPSTRUCT();
      this._ShFile.hwnd = IntPtr.Zero;
      this._ShFile.wFunc = FO_Func.FO_COPY;
      this._ShFile.pFrom = "";
      this._ShFile.pTo = "";
      this._ShFile.fAnyOperationsAborted = false;
      this._ShFile.hNameMappings = IntPtr.Zero;
      this._ShFile.lpszProgressTitle = "";

    }


    /// <summary>
    /// 処理実行
    /// </summary>
    /// <returns>成功したらtrue</returns>
    public bool Execute()
    {
      this._ShFile.fFlags = this.fFlags.Flag;
      int ReturnValue = SHFileOperation(ref this._ShFile);
      if ( ReturnValue == 0 )
      {
        return true;
      }
      else
      {
        return false;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public class FILEOP_FLAGS
    {
      private enum FILEOP_FLAGS_ENUM : ushort
      {
        /// <summary>
        /// The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
        /// </summary>
        FOF_MULTIDESTFILES = 0x0001,
        /// <summary>
        /// Not used.
        /// </summary>
        FOF_CONFIRMMOUSE = 0x0002,
        /// <summary>
        /// Do not display a progress dialog box.
        /// </summary>
        FOF_SILENT = 0x0004,  // don't create progress/report
        /// <summary>
        /// Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
        /// </summary>
        FOF_RENAMEONCOLLISION = 0x0008,
        /// <summary>
        /// Respond with "Yes to All" for any dialog box that is displayed.
        /// </summary>
        FOF_NOCONFIRMATION = 0x0010,  // Don't prompt the user.
        /// <summary>
        /// If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object containing their old and new names to the hNameMappings member.
        /// </summary>
        FOF_WANTMAPPINGHANDLE = 0x0020,  // Fill in SHFILEOPSTRUCT.hNameMappings
        /// <summary>
        /// Preserve undo information, if possible.
        /// Operations can be undone only from the same process that performed the original operation.
        /// If pFrom does not contain fully qualified path and file names, this flag is ignored.
        /// </summary>
        // Must be freed using SHFreeNameMappings
        FOF_ALLOWUNDO = 0x0040,
        /// <summary>
        /// Perform the operation on files only if a wildcard file name (*.*) is specified.
        /// </summary>
        FOF_FILESONLY = 0x0080,  // on *.*, do only files
        /// <summary>
        /// Display a progress dialog box but do not show the file names.
        /// </summary>
        FOF_SIMPLEPROGRESS = 0x0100,  // means don't show names of files
        /// <summary>
        /// Do not confirm the creation of a new directory if the operation requires one to be created.
        /// </summary>
        FOF_NOCONFIRMMKDIR = 0x0200,  // don't confirm making any needed dirs
        /// <summary>
        /// Do not display a user interface if an error occurs.
        /// </summary>
        FOF_NOERRORUI = 0x0400,  // don't put up error UI
        /// <summary>
        /// Version 4.71. Do not copy the security attributes of the file.
        /// </summary>
        FOF_NOCOPYSECURITYATTRIBS = 0x0800,  // dont copy NT file Security Attributes
        /// <summary>
        /// Only operate in the local directory. Don't operate recursively into subdirectories.
        /// </summary>
        FOF_NORECURSION = 0x1000,  // don't recurse into directories.
        /// <summary>
        /// Version 5.0. Do not move connected files as a group. Only move the specified files.
        /// </summary>
        FOF_NO_CONNECTED_ELEMENTS = 0x2000,  // don't operate on connected elements.
        /// <summary>
        /// Version 5.0. Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
        /// </summary>
        FOF_WANTNUKEWARNING = 0x4000,  // during delete operation, warn if nuking instead of recycling (partially overrides FOF_NOCONFIRMATION)
        /// <summary>
        /// Treat reparse points as objects, not containers.
        /// You must set _WIN32_WINNT to 5.01 or later to use this flag.
        /// See Shell and Common Controls Versions for further discussion of versioning.
        /// </summary>
        FOF_NORECURSEREPARSE = 0x8000,  // treat reparse points as objects, not containers
      }

      /// <summary>
      /// The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
      /// </summary>
      public bool FOF_MULTIDESTFILES = false;
      /// <summary>
      /// Not used.
      /// </summary>
      public bool FOF_CONFIRMMOUSE = false;
      /// <summary>
      /// Do not display a progress dialog box.
      /// </summary>
      public bool FOF_SILENT = false;
      /// <summary>
      /// Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
      /// </summary>
      public bool FOF_RENAMEONCOLLISION = false;
      /// <summary>
      /// Respond with "Yes to All" for any dialog box that is displayed.
      /// </summary>
      public bool FOF_NOCONFIRMATION = false;
      /// <summary>
      /// If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object containing their old and new names to the hNameMappings member.
      /// </summary>
      public bool FOF_WANTMAPPINGHANDLE = false;
      /// <summary>
      /// Preserve undo information, if possible.
      /// Operations can be undone only from the same process that performed the original operation.
      /// If pFrom does not contain fully qualified path and file names, this flag is ignored.
      /// </summary>
      public bool FOF_ALLOWUNDO = false;
      /// <summary>
      /// Perform the operation on files only if a wildcard file name (*.*) is specified.
      /// </summary>
      public bool FOF_FILESONLY = false;
      /// <summary>
      /// Display a progress dialog box but do not show the file names.
      /// </summary>
      public bool FOF_SIMPLEPROGRESS = false;
      /// <summary>
      /// Do not confirm the creation of a new directory if the operation requires one to be created.
      /// </summary>
      public bool FOF_NOCONFIRMMKDIR = false;
      /// <summary>
      /// Do not display a user interface if an error occurs.
      /// </summary>
      public bool FOF_NOERRORUI = false;
      /// <summary>
      /// Version 4.71. Do not copy the security attributes of the file.
      /// </summary>
      public bool FOF_NOCOPYSECURITYATTRIBS = false;
      /// <summary>
      /// Only operate in the local directory. Don't operate recursively into subdirectories.
      /// </summary>
      public bool FOF_NORECURSION = false;
      /// <summary>
      /// Version 5.0. Do not move connected files as a group. Only move the specified files.
      /// </summary>
      public bool FOF_NO_CONNECTED_ELEMENTS = false;
      /// <summary>
      /// Version 5.0. Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
      /// </summary>
      public bool FOF_WANTNUKEWARNING = false;
      /// <summary>
      /// Treat reparse points as objects, not containers.
      /// You must set _WIN32_WINNT to 5.01 or later to use this flag.
      /// See Shell and Common Controls Versions for further discussion of versioning.
      /// </summary>
      public bool FOF_NORECURSEREPARSE = false;
      /// <summary>
      /// フラグ
      /// </summary>
      public ushort Flag
      {
        get
        {
          ushort ReturnValue = 0;

          if ( this.FOF_MULTIDESTFILES == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_MULTIDESTFILES;
          if ( this.FOF_CONFIRMMOUSE == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_CONFIRMMOUSE;
          if ( this.FOF_SILENT == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SILENT;
          if ( this.FOF_RENAMEONCOLLISION == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_RENAMEONCOLLISION;
          if ( this.FOF_NOCONFIRMATION == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION;
          if ( this.FOF_WANTMAPPINGHANDLE == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTMAPPINGHANDLE;
          if ( this.FOF_ALLOWUNDO == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_ALLOWUNDO;
          if ( this.FOF_FILESONLY == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_FILESONLY;
          if ( this.FOF_SIMPLEPROGRESS == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SIMPLEPROGRESS;
          if ( this.FOF_NOCONFIRMMKDIR == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMMKDIR;
          if ( this.FOF_NOERRORUI == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOERRORUI;
          if ( this.FOF_NOCOPYSECURITYATTRIBS == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCOPYSECURITYATTRIBS;
          if ( this.FOF_NORECURSION == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSION;
          if ( this.FOF_NO_CONNECTED_ELEMENTS == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NO_CONNECTED_ELEMENTS;
          if ( this.FOF_WANTNUKEWARNING == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTNUKEWARNING;
          if ( this.FOF_NORECURSEREPARSE == true )
            ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSEREPARSE;
          
          return ReturnValue;
        }
      }
    }

  }
}

勉強会: 05/18 大阪 登録受付中

中の技術日誌
コンテンツ
わんくま同盟
わんくま同盟
わんくま同盟
広告
バナー
MVP LOGO
MSMVP Visual C# Since 2004/04-2007/03
MCP LOGO
070-316
姉妹サイト
姉妹サイト:じゃんぬのC#, VB.NET 入門
じゃんぬの
C#, VB.NET 入門
検索
Google

ブログ本家
広告