1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
public class Files
{
public enum FO : int
{
FO_MOVE = 1,
FO_COPY = 2,
FO_DELETE = 3,
FOF_ALLOWUNDO = 0x40,
FOF_NOCONFIRMATION = 0x10, //Don't prompt the user.;
FOF_NOCONFIRMMKDIR = 0x200,
FOF_SILENT = 0x4,
FOF_WANTMAPPINGHANDLE = 0x20,
FOF_RENAMEONCOLLISION = 0x8,
FOF_CONFIRMMOUSE = 0x2,
FOF_FILESONLY = 0x80,
FOF_MULTIDESTFILES = 0x1,
FOF_NO_CONNECTED_ELEMENTS = 0x1000,
FOF_NOCOPYSECURITYATTRIBS = 0x800,
FOF_SIMPLEPROGRESS = 0x100,
FOF_WANTNUKEWARNING = 0x2000,
FOF_NORECURSION = 0x1000,
FOF_NOERRORUI = 0x0400
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
public void CopyFile(string SourceFile, string DestinationFile)
{
const string FromWildcard = @"{0}\*.*";
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = (int)FO.FO_COPY;
shf.fFlags = (int)FO.FOF_NOCONFIRMMKDIRFO /
(int).FOF_WANTMAPPINGHANDLE;
shf.pFrom = string.Format(FromWildcard,SourceFile)+'\0'+'\0';
shf.pTo = DestinationFile + '\0' + '\0';
int res = SHFileOperation(ref shf);
Console.WriteLine(res);
}
} |
Partager