文件拷贝时显示进度条

来源:互联网 发布:sql 保存特殊字符 编辑:程序博客网 时间:2024/05/16 05:26

用 [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
  public static extern Int32 SHFileOperation(ref SHFILEOPSTRUCT lpFileOp); // Address of an SHFILEOPSTRUCT structure that contains information  
    
代码:
private void copy(string[] f1, string[] f2)
  {
  SHFILEOPSTRUCT FileOpStruct = new SHFILEOPSTRUCT();



  uint FO_MOVE = 0x0001;
  uint FO_COPY = 0x0002;
  uint FO_DELETE = 0x0003;
  uint FO_RENAME = 0x0004;


  FileOpStruct.hwnd = IntPtr.Zero;
  FileOpStruct.wFunc = FO_COPY;

  String multiSource = StringArrayToMultiString(f1);
  String multiDest = StringArrayToMultiString(f2);
  FileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource);
  FileOpStruct.pTo = Marshal.StringToHGlobalUni(multiDest);
  FileOpStruct.fFlags = (ushort)ShellFileOperationFlags.FOF_MULTIDESTFILES;
    
  FileOpStruct.lpszProgressTitle = "aaaaaaaa";
  FileOpStruct.fAnyOperationsAborted = 0;
  FileOpStruct.hNameMappings = IntPtr.Zero;

  int RetVal;
  RetVal = SHFileOperation(ref FileOpStruct);

  }

  private String StringArrayToMultiString(String[] stringArray)
  {
  String multiString = "";

  if (stringArray == null)
  return "";

  for (int i = 0; i < stringArray.Length; i++)
  multiString += stringArray[i] + '/0';

  multiString += '/0';

  return multiString;
  }


  [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
  public static extern Int32 SHFileOperation(ref SHFILEOPSTRUCT lpFileOp); // Address of an SHFILEOPSTRUCT structure that contains information  
  public struct SHFILEOPSTRUCT
  {
  public IntPtr hwnd; // Window handle to the dialog box to display information about the  
  // status of the file operation.  
  public UInt32 wFunc; // Value that indicates which operation to perform.
  public IntPtr pFrom; // Address of a buffer to specify one or more source file names.  
  // These names must be fully qualified paths. Standard Microsoft?
  // MS-DOS?wild cards, such as "*", are permitted in the file-name  
  // position. Although this member is declared as a null-terminated  
  // string, it is used as a buffer to hold multiple file names. Each  
  // file name must be terminated by a single NULL character. An
  // additional NULL character must be appended to the end of the  
  // final name to indicate the end of pFrom.  
  public IntPtr pTo; // Address of a buffer to contain the name of the destination file or  
  // directory. This parameter must be set to NULL if it is not used.
  // Like pFrom, the pTo member is also a double-null terminated  
  // string and is handled in much the same way.  
  public UInt16 fFlags; // Flags that control the file operation.  
  public Int32 fAnyOperationsAborted; // Value that receives TRUE if the user aborted any file operations
  // before they were completed, or FALSE otherwise.  
  public IntPtr hNameMappings; // A handle to a name mapping object containing the old and new  
  // names of the renamed files. This member is used only if the  
  // fFlags member includes the FOF_WANTMAPPINGHANDLE flag.
  [MarshalAs(UnmanagedType.LPWStr)]
  public String lpszProgressTitle; // Address of a string to use as the title of a progress dialog box.
  // This member is used only if fFlags includes the  
  // FOF_SIMPLEPROGRESS flag.
  }
  public enum ShellFileOperationFlags
  {
  FOF_MULTIDESTFILES = 0x0001,// The pTo member specifies multiple destination files (one for  
  // each source file) rather than one directory where all source  
  // files are to be deposited.  
  FOF_CONFIRMMOUSE = 0x0002,// Not currently used.  
  FOF_SILENT = 0x0004,// Do not display a progress dialog box.  
  FOF_RENAMEONCOLLISION = 0x0008,// 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.  
  FOF_NOCONFIRMATION = 0x0010,// Respond with "Yes to All" for any dialog box that is displayed.  
  FOF_WANTMAPPINGHANDLE = 0x0020,// 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.
  FOF_ALLOWUNDO = 0x0040,// Preserve Undo information, if possible. If pFrom does not  
  // contain fully qualified path and file names, this flag is ignored.  
  FOF_FILESONLY = 0x0080,// Perform the operation on files only if a wildcard file  
  // name (*.*) is specified.  
  FOF_SIMPLEPROGRESS = 0x0100,// Display a progress dialog box but do not show the file names.  
  FOF_NOCONFIRMMKDIR = 0x0200,// Do not confirm the creation of a new directory if the operation
  // requires one to be created.  
  FOF_NOERRORUI = 0x0400,// Do not display a user interface if an error occurs.  
  FOF_NOCOPYSECURITYATTRIBS = 0x0800,// Do not copy the security attributes of the file.
  FOF_NORECURSION = 0x1000,// Only operate in the local directory. Don't operate recursively
  // into subdirectories.
  FOF_NO_CONNECTED_ELEMENTS = 0x2000,// Do not move connected files as a group. Only move the  
  // specified files.  
  FOF_WANTNUKEWARNING = 0x4000,// Send a warning if a file is being destroyed during a delete  
  // operation rather than recycled. This flag partially  
  // overrides FOF_NOCONFIRMATION.
  FOF_NORECURSEREPARSE = 0x8000 // Treat reparse points as objects, not containers.

  }

原创粉丝点击