-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWin32File.cs
304 lines (269 loc) · 8.78 KB
/
Win32File.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Modified version of Balaji Lakshmanan's Win32File.
// Original here: http://www.codeproject.com/KB/files/LongFileNames.aspx
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.IO;
namespace System.IO
{
class Win32File
{
// Error
const int ERROR_ALREADY_EXISTS = 183;
// seek location
const uint FILE_BEGIN = 0x0;
const uint FILE_CURRENT = 0x1;
const uint FILE_END = 0x2;
// access
const uint GENERIC_READ = 0x80000000;
const uint GENERIC_WRITE = 0x40000000;
const uint GENERIC_EXECUTE = 0x20000000;
const uint GENERIC_ALL = 0x10000000;
const uint FILE_APPEND_DATA = 0x00000004;
// attribute
const uint FILE_ATTRIBUTE_NORMAL = 0x80;
const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF;
// share
const uint FILE_SHARE_DELETE = 0x00000004;
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
//mode
const uint CREATE_NEW = 1;
const uint CREATE_ALWAYS = 2;
const uint OPEN_EXISTING = 3;
const uint OPEN_ALWAYS = 4;
const uint TRUNCATE_EXISTING = 5;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern SafeFileHandle CreateFileW(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint SetFilePointer(SafeFileHandle hFile, long lDistanceToMove, IntPtr lpDistanceToMoveHigh, uint dwMoveMethod);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint GetFileAttributesW(string lpFileName);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint MoveFileW(string lpExistingFileName, string lpNewFileName);
// uint GetMode( FileMode mode )
// Converts the filemode constant to win32 constant
#region GetMode
private static uint GetMode(FileMode mode)
{
uint umode = 0;
switch (mode)
{
case FileMode.CreateNew:
umode = CREATE_NEW;
break;
case FileMode.Create:
umode = CREATE_ALWAYS;
break;
case FileMode.Append:
umode = OPEN_ALWAYS;
break;
case FileMode.Open:
umode = OPEN_EXISTING;
break;
case FileMode.OpenOrCreate:
umode = OPEN_ALWAYS;
break;
case FileMode.Truncate:
umode = TRUNCATE_EXISTING;
break;
}
return umode;
}
#endregion
// uint GetAccess(FileAccess access)
// Converts the FileAccess constant to win32 constant
#region GetAccess
private static uint GetAccess(FileAccess access)
{
uint uaccess = 0;
switch (access)
{
case FileAccess.Read:
uaccess = GENERIC_READ;
break;
case FileAccess.ReadWrite:
uaccess = GENERIC_READ | GENERIC_WRITE;
break;
case FileAccess.Write:
uaccess = GENERIC_WRITE;
break;
}
return uaccess;
}
#endregion
// uint GetShare(FileShare share)
// Converts the FileShare constant to win32 constant
#region GetShare
private static uint GetShare(FileShare share)
{
uint ushare = 0;
switch (share)
{
case FileShare.Read:
ushare = FILE_SHARE_READ;
break;
case FileShare.ReadWrite:
ushare = FILE_SHARE_READ | FILE_SHARE_WRITE;
break;
case FileShare.Write:
ushare = FILE_SHARE_WRITE;
break;
case FileShare.Delete:
ushare = FILE_SHARE_DELETE;
break;
case FileShare.None:
ushare = 0;
break;
}
return ushare;
}
#endregion
public static string LongFileName(string filepath)
{
// If file path is disk file path then prepend it with \\?\
// if file path is UNC prepend it with \\?\UNC\ and remove \\ prefix in unc path.
if (filepath.StartsWith(@"\\"))
return @"\\?\UNC\" + filepath.Substring(2, filepath.Length - 2);
else
return @"\\?\" + filepath;
}
public static FileStream Open(string filepath, FileMode mode)
{
//opened in the specified mode and path, with read/write access and not shared
FileStream fs = null;
uint umode = GetMode(mode);
uint uaccess = GENERIC_READ | GENERIC_WRITE;
uint ushare = 0; //not shared
if (mode == FileMode.Append)
uaccess = FILE_APPEND_DATA;
SafeFileHandle sh = CreateFileW(LongFileName(filepath), uaccess, ushare, IntPtr.Zero, umode, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
int iError = Marshal.GetLastWin32Error();
if (/*(iError > 0 && !(mode == FileMode.Append && iError == ERROR_ALREADY_EXISTS)) || */sh.IsInvalid)
{
throw new Exception("Error opening file Win32 Error:" + iError);
}
else
{
fs = new FileStream(sh, FileAccess.ReadWrite);
}
// if opened in append mode
if (mode == FileMode.Append)
{
if (!sh.IsInvalid)
{
SetFilePointer(sh, 0, IntPtr.Zero, FILE_END);
}
}
return fs;
}
public static FileStream Open(string filepath, FileMode mode, FileAccess access)
{
//opened in the specified mode and access and not shared
FileStream fs = null;
uint umode = GetMode(mode);
uint uaccess = GetAccess(access);
uint ushare = 0;//not shared
if (mode == FileMode.Append)
uaccess = FILE_APPEND_DATA;
SafeFileHandle sh = CreateFileW(LongFileName(filepath), uaccess, ushare, IntPtr.Zero, umode, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
int iError = Marshal.GetLastWin32Error();
if ((iError > 0 && !(mode == FileMode.Append && iError != ERROR_ALREADY_EXISTS)) || sh.IsInvalid)
{
throw new Exception("Error opening file Win32 Error:" + iError);
}
else
{
fs = new FileStream(sh, access);
}
// if opened in append mode
if (mode == FileMode.Append)
{
if (!sh.IsInvalid)
{
SetFilePointer(sh, 0, IntPtr.Zero, FILE_END);
}
}
return fs;
}
public static FileStream Open(string filepath, FileMode mode, FileAccess access, FileShare share)
{
//opened in the specified mode , access and share
FileStream fs = null;
uint umode = GetMode(mode);
uint uaccess = GetAccess(access);
uint ushare = GetShare(share);
if (mode == FileMode.Append)
uaccess = FILE_APPEND_DATA;
SafeFileHandle sh = CreateFileW(LongFileName(filepath), uaccess, ushare, IntPtr.Zero, umode, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
int iError = Marshal.GetLastWin32Error();
if ((iError > 0 && !(mode == FileMode.Append && iError != ERROR_ALREADY_EXISTS)) || sh.IsInvalid)
{
throw new Exception("Error opening file Win32 Error:" + iError);
}
else
{
fs = new FileStream(sh, access);
}
// if opened in append mode
if (mode == FileMode.Append)
{
if (!sh.IsInvalid)
{
SetFilePointer(sh, 0, IntPtr.Zero, FILE_END);
}
}
return fs;
}
public static FileStream OpenRead(string filepath)
{
// Open readonly file mode open(String, FileMode.Open, FileAccess.Read, FileShare.Read)
return Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
public static FileStream OpenWrite(string filepath)
{
//open writable open(String, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None).
return Open(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
public static bool Exists(string filepath)
{
uint attribs = GetFileAttributesW(LongFileName(filepath));
return (attribs != INVALID_FILE_ATTRIBUTES) && ((attribs & FILE_ATTRIBUTE_DIRECTORY) == 0);
}
public static void Move(string oldName, string newName)
{
MoveFileW(LongFileName(oldName), LongFileName(newName));
}
}
class Win32Directory
{
// file attributes
const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool CreateDirectoryW(string lpPathName, IntPtr lpSecurityAttributes);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint GetFileAttributesW(string lpFileName);
public static void CreateDirectory(string path)
{
if (path[path.Length-1]!='\\')
path += '\\';
for (int i=0; i<path.Length; i++)
if (path[i]=='\\')
{
string target = @"\\?\" + path.Substring(0, i);
CreateDirectoryW(target, IntPtr.Zero);
}
}
public static bool Exists(string filepath)
{
uint attribs = GetFileAttributesW(Win32File.LongFileName(filepath));
return (attribs != INVALID_FILE_ATTRIBUTES) && ((attribs & FILE_ATTRIBUTE_DIRECTORY) != 0);
}
}
}