Moving files around.
2 // Copyright (C) 2014-2015 Stéphane Lenclud.
4 // This file is part of SharpLibHid.
6 // SharpDisplayManager is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // SharpDisplayManager is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with SharpDisplayManager. If not, see <http://www.gnu.org/licenses/>.
21 using System.Runtime.InteropServices;
22 using Microsoft.Win32.SafeHandles;
24 namespace SharpLib.Win32
27 static partial class Function
29 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
30 public static extern SafeFileHandle CreateFile(
31 [MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
32 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
33 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
34 IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
35 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
36 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
37 IntPtr hTemplateFile);
39 [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
40 public static extern SafeFileHandle CreateFileA(
41 [MarshalAs(UnmanagedType.LPStr)] string lpFileName,
42 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
43 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
44 IntPtr lpSecurityAttributes,
45 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
46 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
47 IntPtr hTemplateFile);
49 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
50 public static extern SafeFileHandle CreateFileW(
51 [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
52 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
53 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
54 IntPtr lpSecurityAttributes,
55 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
56 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
57 IntPtr hTemplateFile);
61 static partial class Macro
68 static partial class Const
74 enum FileAccess : uint
78 GENERIC_ALL = 0x10000000,
79 GENERIC_EXECUTE = 0x20000000,
80 GENERIC_READ = 0x80000000,
81 GENERIC_WRITE = 0x40000000,
83 FILE_READ_DATA = (0x0001), // file & pipe
84 FILE_LIST_DIRECTORY = (0x0001), // directory
86 FILE_WRITE_DATA = (0x0002), // file & pipe
87 FILE_ADD_FILE = (0x0002), // directory
89 FILE_APPEND_DATA = (0x0004), // file
90 FILE_ADD_SUBDIRECTORY = (0x0004), // directory
91 FILE_CREATE_PIPE_INSTANCE = (0x0004), // named pipe
93 FILE_READ_EA = (0x0008), // file & directory
95 FILE_WRITE_EA = (0x0010), // file & directory
97 FILE_EXECUTE = (0x0020), // file
98 FILE_TRAVERSE = (0x0020), // directory
100 FILE_DELETE_CHILD = (0x0040), // directory
102 FILE_READ_ATTRIBUTES = (0x0080), // all
104 FILE_WRITE_ATTRIBUTES = (0x0100), // all
106 FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF),
108 FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
109 FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
110 FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE),
112 DELETE = (0x00010000),
113 READ_CONTROL = (0x00020000),
114 WRITE_DAC = (0x00040000),
115 WRITE_OWNER = (0x00080000),
116 SYNCHRONIZE = (0x00100000),
118 STANDARD_RIGHTS_REQUIRED = (0x000F0000),
120 STANDARD_RIGHTS_READ = (READ_CONTROL),
121 STANDARD_RIGHTS_WRITE = (READ_CONTROL),
122 STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
124 STANDARD_RIGHTS_ALL = (0x001F0000),
126 SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
128 ACCESS_SYSTEM_SECURITY = (0x01000000),
130 MAXIMUM_ALLOWED = (0x02000000)
136 public enum FileShare : uint
139 /// Prevents other processes from opening a file or device if they request delete, read, or write access.
141 FILE_SHARE_NONE = 0x00000000,
143 /// Enables subsequent open operations on an object to request read access.
144 /// Otherwise, other processes cannot open the object if they request read access.
145 /// If this flag is not specified, but the object has been opened for read access, the function fails.
147 FILE_SHARE_READ = 0x00000001,
149 /// Enables subsequent open operations on an object to request write access.
150 /// Otherwise, other processes cannot open the object if they request write access.
151 /// If this flag is not specified, but the object has been opened for write access, the function fails.
153 FILE_SHARE_WRITE = 0x00000002,
155 /// Enables subsequent open operations on an object to request delete access.
156 /// Otherwise, other processes cannot open the object if they request delete access.
157 /// If this flag is not specified, but the object has been opened for delete access, the function fails.
159 FILE_SHARE_DELETE = 0x00000004
162 public enum CreationDisposition : uint
165 /// Creates a new file. The function fails if a specified file exists.
169 /// Creates a new file, always.
170 /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
171 /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
175 /// Opens a file. The function fails if the file does not exist.
179 /// Opens a file, always.
180 /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
184 /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
185 /// The calling process must open the file with the GENERIC_WRITE access right.
187 TRUNCATE_EXISTING = 5
191 public enum FileFlagsAttributes : uint
193 FILE_ATTRIBUTE_READONLY = 0x00000001,
194 FILE_ATTRIBUTE_HIDDEN = 0x00000002,
195 FILE_ATTRIBUTE_SYSTEM = 0x00000004,
196 FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
197 FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
198 FILE_ATTRIBUTE_DEVICE = 0x00000040,
199 FILE_ATTRIBUTE_NORMAL = 0x00000080,
200 FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
201 FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
202 FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
203 FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
204 FILE_ATTRIBUTE_OFFLINE = 0x00001000,
205 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
206 FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
207 FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000,
208 FILE_ATTRIBUTE_VIRTUAL = 0x00010000,
209 FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000,
210 // These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond)
211 FILE_FLAG_WRITE_THROUGH = 0x80000000,
212 FILE_FLAG_OVERLAPPED = 0x40000000,
213 FILE_FLAG_NO_BUFFERING = 0x20000000,
214 FILE_FLAG_RANDOM_ACCESS = 0x10000000,
215 FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
216 FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
217 FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
218 FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
219 FILE_FLAG_SESSION_AWARE = 0x00800000,
220 FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
221 FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
222 FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000