Method OpenHandle
OpenHandle(FileMode, FileAccess, FileShare, FileOptions, long)
Opens a handle to a new or existing file, for use with the methods of RandomAccess.
Declaration
SafeFileHandle OpenHandle(FileMode mode = FileMode.Open, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.None, FileOptions options = FileOptions.None, long preallocationSize = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| FileMode | mode | A FileMode constant specifying the mode (for example, Open or Append) in which to open the file. |
| FileAccess | access | A FileAccess constant specifying whether to open the file with |
| FileShare | share | A FileShare constant specifying the type of access other handles have to this file. |
| FileOptions | options | Additional file options. |
| long | preallocationSize | The initial allocation size in bytes for the file. Must be at least zero. |
Returns
| Type | Description |
|---|---|
| SafeFileHandle | A new SafeFileHandle to the opened file. |
Remarks
A handle has no position and no buffer: each read and write through RandomAccess names its own offset in the file and goes straight to the operating system. That makes it lighter than the FileStream that OpenStream(FileMode, FileAccess, FileShare, int, FileOptions) returns where the file is read or written in a few large pieces, and heavier where it is read or written in many small ones.
preallocationSize is a hint that only has an effect when a file is created or overwritten with write access. The file's length
stays zero until it is written to.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
| ArgumentException | The combination of |