0% found this document useful (0 votes)
2 views

complete-reference-vb_net_43

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

complete-reference-vb_net_43

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File Enumerations

Dim noiseFile As New FileStream(filePath, FileMode.Open, _


FileAccess.Read, FileShare.Read)
• OpenOrCreate A useful attribute if you are creating a number of files. Use this attribute with
FileAccess.Read and FileIOPermissionAccess.Read. When you use FileAccess.ReadWrite and the
file exists, FileIOPermissionAccess.Write is required at the same time. But if the file does not exist,
FileIOPermissionAccess.Append is required in addition to Read and Write. The following example
shows this happening:

Dim noiseFile As New FileStream(filePath, FileMode.OpenOrCreate, _


FileAccess.Read, FileShare.Read)
• Truncate This attribute will cause an existing file to be opened and cleared or flushed in one pass. In
other words, as soon as it is opened, the file size of the file is zero bytes. This operation requires
FileIOPermissionAccess.Write. Naturally, any attempts to read from a truncated file will result in an
exception. The following method opens a file and specifies truncation:

Dim noiseFile As New FileStream(filePath, FileMode.Truncate, _


FileAccess.Read, FileShare.Read)

Note If a file is already open when you try to open it using one of the Read, Write, or None flags, the
operation will fail. You can only gain access to the file once the current owner has closed it. And
even if the file is closed and you pass one of the above flags, you may still need additional
permissions to access it.

FileShare

The constants exposed in the FileShare enumeration map to constants that let you specify to the file system
exactly how a file should be opened when it opens it. These constants are typically passed to the Open
methods of File and FileInfo and in the constructors of FileStream (discussed later in this chapter) and the
IsolateStorageFileStream. Table 15−15 lists the constants of this enumeration.

Basic File Class Operations

This section demonstrates how to create and work with files. In the example code, I have created a class with
various methods that call the File class's static methods. I then allow other objects to delegate to this wrapper
or bridge the objects for file operations.

Table 15−15: Constants for the FileShare Parameter

Member Purpose
Inheritable Allows the file handle to be inherited by child processes. This feature is apparently
not directly supported by the Win32 API.
None Rebukes attempts to share access to a file. Any request to open the file by the current
process or any another process fails until the file is closed.
Read Allows subsequent opening of the file for reading
ReadWrite Allows subsequent opening of the file for reading or writing
Write Allows subsequent opening of the file for writing

527

You might also like