Now getting manufacturer and product name.
2 using System.Runtime.InteropServices;
3 using Microsoft.Win32.SafeHandles;
8 static partial class Function
10 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
11 public static extern SafeFileHandle CreateFile(
12 [MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
13 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
14 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
15 IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
16 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
17 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
18 IntPtr hTemplateFile);
20 [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
21 public static extern SafeFileHandle CreateFileA(
22 [MarshalAs(UnmanagedType.LPStr)] string lpFileName,
23 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
24 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
25 IntPtr lpSecurityAttributes,
26 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
27 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
28 IntPtr hTemplateFile);
30 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
31 public static extern SafeFileHandle CreateFileW(
32 [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
33 [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
34 [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
35 IntPtr lpSecurityAttributes,
36 [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
37 [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
38 IntPtr hTemplateFile);
42 static partial class Macro
49 static partial class Const
55 enum FileAccess : uint
59 GENERIC_ALL = 0x10000000,
60 GENERIC_EXECUTE = 0x20000000,
61 GENERIC_READ = 0x80000000,
62 GENERIC_WRITE = 0x40000000,
64 FILE_READ_DATA = (0x0001), // file & pipe
65 FILE_LIST_DIRECTORY = (0x0001), // directory
67 FILE_WRITE_DATA = (0x0002), // file & pipe
68 FILE_ADD_FILE = (0x0002), // directory
70 FILE_APPEND_DATA = (0x0004), // file
71 FILE_ADD_SUBDIRECTORY = (0x0004), // directory
72 FILE_CREATE_PIPE_INSTANCE = (0x0004), // named pipe
74 FILE_READ_EA = (0x0008), // file & directory
76 FILE_WRITE_EA = (0x0010), // file & directory
78 FILE_EXECUTE = (0x0020), // file
79 FILE_TRAVERSE = (0x0020), // directory
81 FILE_DELETE_CHILD = (0x0040), // directory
83 FILE_READ_ATTRIBUTES = (0x0080), // all
85 FILE_WRITE_ATTRIBUTES = (0x0100), // all
87 FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF),
89 FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
90 FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
91 FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE),
93 DELETE = (0x00010000),
94 READ_CONTROL = (0x00020000),
95 WRITE_DAC = (0x00040000),
96 WRITE_OWNER = (0x00080000),
97 SYNCHRONIZE = (0x00100000),
99 STANDARD_RIGHTS_REQUIRED = (0x000F0000),
101 STANDARD_RIGHTS_READ = (READ_CONTROL),
102 STANDARD_RIGHTS_WRITE = (READ_CONTROL),
103 STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
105 STANDARD_RIGHTS_ALL = (0x001F0000),
107 SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
109 ACCESS_SYSTEM_SECURITY = (0x01000000),
111 MAXIMUM_ALLOWED = (0x02000000)
117 public enum FileShare : uint
120 /// Prevents other processes from opening a file or device if they request delete, read, or write access.
122 FILE_SHARE_NONE = 0x00000000,
124 /// Enables subsequent open operations on an object to request read access.
125 /// Otherwise, other processes cannot open the object if they request read access.
126 /// If this flag is not specified, but the object has been opened for read access, the function fails.
128 FILE_SHARE_READ = 0x00000001,
130 /// Enables subsequent open operations on an object to request write access.
131 /// Otherwise, other processes cannot open the object if they request write access.
132 /// If this flag is not specified, but the object has been opened for write access, the function fails.
134 FILE_SHARE_WRITE = 0x00000002,
136 /// Enables subsequent open operations on an object to request delete access.
137 /// Otherwise, other processes cannot open the object if they request delete access.
138 /// If this flag is not specified, but the object has been opened for delete access, the function fails.
140 FILE_SHARE_DELETE = 0x00000004
143 public enum CreationDisposition : uint
146 /// Creates a new file. The function fails if a specified file exists.
150 /// Creates a new file, always.
151 /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
152 /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
156 /// Opens a file. The function fails if the file does not exist.
160 /// Opens a file, always.
161 /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
165 /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
166 /// The calling process must open the file with the GENERIC_WRITE access right.
168 TRUNCATE_EXISTING = 5
172 public enum FileFlagsAttributes : uint
174 FILE_ATTRIBUTE_READONLY = 0x00000001,
175 FILE_ATTRIBUTE_HIDDEN = 0x00000002,
176 FILE_ATTRIBUTE_SYSTEM = 0x00000004,
177 FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
178 FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
179 FILE_ATTRIBUTE_DEVICE = 0x00000040,
180 FILE_ATTRIBUTE_NORMAL = 0x00000080,
181 FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
182 FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
183 FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
184 FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
185 FILE_ATTRIBUTE_OFFLINE = 0x00001000,
186 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
187 FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
188 FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000,
189 FILE_ATTRIBUTE_VIRTUAL = 0x00010000,
190 FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000,
191 // These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond)
192 FILE_FLAG_WRITE_THROUGH = 0x80000000,
193 FILE_FLAG_OVERLAPPED = 0x40000000,
194 FILE_FLAG_NO_BUFFERING = 0x20000000,
195 FILE_FLAG_RANDOM_ACCESS = 0x10000000,
196 FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
197 FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
198 FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
199 FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
200 FILE_FLAG_SESSION_AWARE = 0x00800000,
201 FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
202 FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
203 FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000