Copy long path directory using unicode version CreateDirectory...
1
public class Kernel322

{3
public const int MAX_PATH = 260;4
public const uint MAXDWORD = 0xFFFFFFFF;5
public const int INVALID_HANDLE_VALUE = -1;6
public const int FILE_ATTRIBUTE_ARCHIVE = 0x20;7
public const int FILE_ATTRIBUTE_DIRECTORY = 0x10;8
public const int FILE_ATTRIBUTE_HIDDEN = 0x2;9
public const int FILE_ATTRIBUTE_NORMAL = 0x80;10
public const int FILE_ATTRIBUTE_READONLY = 0x1;11
public const int FILE_ATTRIBUTE_SYSTEM = 0x4;12
public const int FILE_ATTRIBUTE_TEMPORARY = 0x100;13
public const int ERROR_NO_MORE_FILES = 18;14

15
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]16
public static extern IntPtr FindFirstFileW([MarshalAs(UnmanagedType.LPWStr)] String lpFileName, [In, Out] ref WIN32_FIND_DATAW lpFindFileData);17

18
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]19
public static extern Boolean FindNextFileW(IntPtr hFindFile, [In, Out] ref WIN32_FIND_DATAW lpFindFileData);20

21
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]22
public static extern int CopyFileW([MarshalAs(UnmanagedType.LPWStr)] String lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] String lpNewFileName, int bFailIfExists);23

24
[DllImport("kernel32.dll", SetLastError = true)]25
public static extern int FindClose(IntPtr handleFile);26

27
[DllImport("kernel32.dll", SetLastError = true)]28
public static extern int CreateDirectoryW([MarshalAs(UnmanagedType.LPWStr)]String directoryName, [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES securityAttributes);29
}30

31

32
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode), Serializable]33
public struct WIN32_FIND_DATAW34

{35
[FieldOffset(0)]36
public int dwFileAttributes;37
[FieldOffset(4)]38
public long ftCreationTime;39
[FieldOffset(12)]40
public long ftLastAccessTime;41
[FieldOffset(20)]42
public long ftLastWriteTime;43
[FieldOffset(28)]44
public int nFileSizeHigh;45
[FieldOffset(32)]46
public int nFileSizeLow;47
[FieldOffset(36)]48
public int dwReserved0;49
[FieldOffset(40)]50
public int dwReserved1;51
[FieldOffset(44)]52
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 520)]53
public String cFileName;54
[FieldOffset(564)]55
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 28)]56
public String cAlternateFileName;57
}58

59
[StructLayout(LayoutKind.Sequential)]60
public class SECURITY_ATTRIBUTES61

{62
[MarshalAs(UnmanagedType.U4)]63
public int nLength;64
public IntPtr lpSecurityDescriptor;65
public Boolean bInheritHandle;66
}67

68
public class ShellCopyDirectory69

{70

/**//// <summary>71
/// Copy current folder to dest folder, like Copy C:\temp\src to D:\temp\des, at last the result is D:\temp\des\src
72
/// </summary>73
/// <param name="src">src folder path</param>74
/// <param name="des">des folder path</param>75
/// <returns></returns>76
public static Boolean CopyDirectory(String src, String des)77

{78
if (!src.StartsWith("\\\\"))79

{80
src = "\\\\?\\" + src;81
}82

83
if (!src.EndsWith("\\"))84

{85
src += "\\";86
}87

88
if (!des.StartsWith("\\\\"))89

{90
des = "\\\\?\\" + des;91
}92

93
if (!des.EndsWith("\\"))94

{95
des += "\\";96
}97
String currentFolder = src.Substring(src.LastIndexOf("\\", src.Length - 2) + 1);98
if ((!currentFolder.EndsWith(":\\")) && (!currentFolder.EndsWith("?\\")))99

{100
des += currentFolder;101
}102

103
SECURITY_ATTRIBUTES lpSecurityAttributes = new SECURITY_ATTRIBUTES();104
DirectorySecurity security = new DirectorySecurity();105
security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));106
lpSecurityAttributes.nLength = Marshal.SizeOf(lpSecurityAttributes);107
byte[] byteSrc = security.GetSecurityDescriptorBinaryForm();108
IntPtr IntPtrDest = Marshal.AllocHGlobal(byteSrc.Length);109
Marshal.Copy(byteSrc, 0, IntPtrDest, byteSrc.Length);110
lpSecurityAttributes.lpSecurityDescriptor = IntPtrDest;111
lpSecurityAttributes.bInheritHandle = true;112
Kernel32.CreateDirectoryW(des, lpSecurityAttributes);113

114
String srcFile = String.Empty;115
String desFile = String.Empty;116
IntPtr hFile = IntPtr.Zero;117
WIN32_FIND_DATAW findData = new WIN32_FIND_DATAW();118
hFile = Kernel32.FindFirstFileW(src + "*", ref findData);119
if (hFile.ToInt32() != Kernel32.INVALID_HANDLE_VALUE)120

{121
do122

{123
if (findData.cFileName[0] != '.')124

{125
srcFile = src + findData.cFileName;126
desFile = des + findData.cFileName;127
//desFile = des;128
if ((findData.dwFileAttributes & Kernel32.FILE_ATTRIBUTE_DIRECTORY) != 0)129

{130
if (!CopyDirectory(srcFile, des))131

{132
Console.WriteLine("CopyDirectory {0} to {1} error, error code is {2}", srcFile, desFile, Marshal.GetLastWin32Error());133
return false;134
}135
}136
else137

{138
if (Kernel32.CopyFileW(srcFile, desFile, 0) == 0)139

{140
Console.WriteLine("CopyFile {0} to {1} error, error code is {2}", srcFile, desFile, Marshal.GetLastWin32Error());141
return false;142
}143
}144
}145
Console.WriteLine(findData.cFileName);146
Console.WriteLine(Marshal.GetLastWin32Error());147
}148
while (Kernel32.FindNextFileW(hFile, ref findData) == true);149

150
Kernel32.FindClose(hFile);151
int error = Marshal.GetLastWin32Error();152
if (error != Kernel32.ERROR_NO_MORE_FILES)153

{154
Console.WriteLine("Get last error no is {0}", error);155
return false;156
}157
}158
return true;159
}160
}
by long ![]()

浙公网安备 33010602011771号