Kernel32CreateFileA(String, Int32, FileShare, IntPtr, FileMode, UInt32, IntPtr) Method

Creates or opens a file or I/O device.

The most commonly used I/O devices are as follows:

file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe.

The function returns a handle that can be used to access the file or device for various types of I/O depending on the file or device and the flags and attributes specified.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, 
	ThrowOnUnmappableChar = true, SetLastError = true)]
public static SafeFileHandle CreateFileA(
	string filepath,
	int access,
	FileShare share,
	IntPtr securityAttributes,
	FileMode creationDisposition,
	uint flagsAndAttributes,
	IntPtr templateFile
)

Parameters

filepath  String
The name of the file or device to be created or opened.

In the ANSI version of this function (CreateFileA(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr)), the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function (CreateFileW(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr)) and prepend "\\?\" to the path, for example: CreateFileW("\\?\C:\Very Long Path\File.txt")

access  Int32
The requested access to the file or device, which can be summarized as read, write, both or neither zero.
share  FileShare
The requested sharing mode of the file or device, which can be read, write, both, delete, all of these, or none (refer to the following table).

Access requests to attributes or extended attributes are not affected by this flag.

securityAttributes  IntPtr
A pointer to a SECURITY_ATTRIBUTES structure that contains two separate but related data members: an optional security descriptor, and a Boolean value that determines whether the returned handle can be inherited by child processes.
creationDisposition  FileMode
An action to take on a file or device that exists or does not exist.
flagsAndAttributes  UInt32
The file or device attributes and flags.
templateFile  IntPtr
A valid handle to a template file with the GENERIC_READ access right.

The template file supplies file attributes and extended attributes for the file that is being created.

Return Value

SafeFileHandle
If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.

If the function fails, the return value is a INVALID_HANDLE_VALUE value (IntPtr(-1)).

To get extended error information, call GetLastWin32Error.

Remarks

See Also