- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace Learn
- {
- public partial class Form1 : Form
- {
- [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- private static extern bool SHFileOperation([In, Out] SHFILEOPSTRUCT str);
- private const int FO_MOVE = 0x1;
- private const int FO_COPY = 0x2;
- private const int FO_DELETE = 0x3;
- private const ushort FOF_NOCONFIRMATION = 0x10;
- private const ushort FOF_ALLOWUNDO = 0x40;
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public class SHFILEOPSTRUCT
- {
- public IntPtr hwnd;
- /// <summary>
- /// 设置操作方式,移动:FO_MOVE,复制:FO_COPY,删除:FO_DELETE
- /// </summary>
- public UInt32 wFunc;
- /// <summary>
- /// 源文件路径
- /// </summary>
- public string pFrom;
- /// <summary>
- /// 目标文件路径
- /// </summary>
- public string pTo;
- /// <summary>
- /// 允许恢复
- /// </summary>
- public UInt16 fFlags;
- /// <summary>
- /// 监测有无中止
- /// </summary>
- public Int32 fAnyOperationsAborted;
- public IntPtr hNameMappings;
- /// <summary>
- /// 设置标题
- /// </summary>
- public string lpszProgressTitle;
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- MessageBox.Show(CopyFile().ToString());
- }
- private bool CopyFile()
- {
- SHFILEOPSTRUCT pm = new SHFILEOPSTRUCT();
- pm.wFunc = FO_COPY;
- pm.pFrom = @"F:/Office2003sp2_xunchi.exe";
- pm.pTo = @"c:/a.exe";
- pm.fFlags = FOF_ALLOWUNDO;//允许恢复
- pm.lpszProgressTitle = "哈哈";
- return !SHFileOperation(pm);
- }
- }
- }