迷失的石头
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
11 随笔 :: 1 文章 :: 2 评论 :: 0 引用
Copy long path directory using unicode version CreateDirectory...
CopyDirectory
1
public
class
Kernel32
2
{
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_DATAW
34
{
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_ATTRIBUTES
61
{
62
[MarshalAs(UnmanagedType.U4)]
63
public
int
nLength;
64
public
IntPtr lpSecurityDescriptor;
65
public
Boolean bInheritHandle;
66
}
67
68
public
class
ShellCopyDirectory
69
{
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
do
122
{
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
else
137
{
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
0
0
0
(请您对文章做出评价)
«
上一篇:
about lpCurrentDirectory of CreateProcess parameters
»
下一篇:
Windows Server Protocols (WSPP)
posted on 2009-01-13 18:00
xiaopohai_long
阅读(144)
评论(1)
编辑
收藏
评论
1431238
#1楼
[
楼主
]
2009-01-14 20:23
xiaopohai_long
modify this struct to support X64
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode), Serializable]
public struct WIN32_FIND_DATAW64
{
//[FieldOffset(0)]
public UInt32 dwFileAttributes;
//[FieldOffset(4)]
public UInt32 ftCreationTime;
//[FieldOffset(8)]
public UInt32 ftCreationTime2;
//[FieldOffset(12)]
public UInt32 ftLastAccessTime;
//[FieldOffset(16)]
public UInt32 ftLastAccessTime2;
//[FieldOffset(20)]
public UInt32 ftLastWriteTime;
//[FieldOffset(24)]
public UInt32 ftLastWriteTime2;
//[FieldOffset(28)]
public UInt32 nFileSizeHigh;
//[FieldOffset(32)]
public UInt32 nFileSizeLow;
//[FieldOffset(36)]
public UInt32 dwReserved0;
//[FieldOffset(40)]
public UInt32 dwReserved1;
//[FieldOffset(48)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 520)]
public String cFileName;
//[FieldOffset(564)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 28)]
public String cAlternateFileName;
}
回复
引用
查看
注册用户登录后才能发表评论,请
登录
或
注册
。
博客园首页
IT新闻
闪存
知识库
招聘
找优秀程序员,就在博客园
IT新闻
:
·
市值最高的10大科技公司 微软技压苹果谷歌
·
苹果iPad已预售出“数十万部” ,前三月销量有望超iPhone
·
微软:Windows 7 SP1将只包含相当少的改进
·
微软:Windows Phone 7用户根本不需要复制和粘贴
·
微软调整业务战略 不再敌视开源社区
每天10分钟,轻松学英语
专题:
Android
iPad
jQuery
Windows 7
推荐职位
:
网站导航:
博客园首页
IT新闻
个人主页
闪存
程序员招聘
社区
博问
网摘
China-pub 计算机图书网上专卖店!6.5万品种2-8折!
China-Pub 计算机绝版图书按需印刷服务
在知识库中查看:
Copy long path directory using unicode version CreateDirectory...
<
2009年1月
>
日
一
二
三
四
五
六
28
29
30
31
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
1
2
3
4
5
6
7
公告
我的主页
个人资料
我的闪存
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
随笔档案
(11)
2009年3月 (1)
2009年1月 (3)
2008年12月 (1)
2008年11月 (4)
2008年10月 (2)
最新随笔
1. Windows Server Protocols (WSPP)
2. Copy long path directory using unicode version CreateDirectory...
3. about lpCurrentDirectory of CreateProcess parameters
4. about checksum of .Net Assembly
5. Version
6. X509Certificate2 .ctor
7. 控件多线程访问的问题
8. About Java's SslSocket.close method
9. About XmlSerializer
10. check method
最新评论
1. re: Copy long path directory using unicode version CreateDirectory...
modify this struct to support X64 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)...
--xiaopohai_long
2. re: X509Certificate2 .ctor
bug:[WMI][SSL]exception:System.Security.Cryptography.CryptographicException: The requested operation...
--xiaopohai_long
阅读排行榜
1. Copy long path directory using unicode version CreateDirectory...(144)
2. X509Certificate2 .ctor(141)
3. About XmlSerializer(141)
4. 控件多线程访问的问题(68)
5. about checksum of .Net Assembly (48)
评论排行榜
1. Copy long path directory using unicode version CreateDirectory...(1)
2. X509Certificate2 .ctor(1)
3. 控件多线程访问的问题(0)
4. About Java's SslSocket.close method(0)
5. About XmlSerializer(0)