C#. The FileStream class. Enumeration FileOptions

The FileStream class. Additional read/write options. Enumeration FileOptions. Examples

This topic is a continuation of the topic:


Contents


Search other resources:




1. Enumeration FileOptions. The purpose

The FileStream class has a number of constructors that you can use to set different modes of file access. According to the documentation, one of the constructors of the FileStream class looks like this:

public FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options)

here

  • path – file path or file name;
  • mode – a constant from the FileMode enumeration that specifies the file open mode. You can read more about the FileMode enumeration here;
  • access – a constant from the FileAccess enumeration that specifies how the file is accessed. You can read more about the FileAccess enumeration here;
  • share – a value from the FileShare enumeration that determines whether the file can be used as a shared resource by various processes;
  • bufferSize – the size of the buffer used for portionwise writing/reading of data in the file. By default, bufferSize = 4096;
  • options – a constant from the FileOptions enumeration that specifies the various options for using the file.

The FileOptions enumeration implements additional features (options) that determine the purpose of an open file:

  • Asynchronous – indicates that the file can be used for asynchronous reading and writing;
  • DeleteOnClose – indicates that the file will be deleted after it is no longer used;
  • Encrypted – indicates that the file is encrypted and can only be decrypted using the same user account used for encryption;
  • None – indicates that no additional options apply to the generated FileStream object;
  • RandomAccess – indicates that the file is being accessed randomly. This hint gives the system a hint to optimize file caching;
  • SequentialScan – indicates that the file is being read sequentially from start to finish. This hint can be used as a hint to the system to optimize file caching. This may provide some performance improvement;
  • WriteThrough – instructing the system to write through any intermediate cache and go directly to disk.

 

2. Examples of processing (writing / reading) a file with specified additional options FileOptions
2.1. Creating a file using options from the FileOptions enumeration

The code snippet creates a file for asynchronous reading and writing. The buffer size is set to 8192 bytes. The file can be used for asynchronous reading and writing.

...

// Create a file for asynchronous reading and writing
using (FileStream fs = new FileStream("file.bin", FileMode.Create,
FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.Asynchronous))
{
  // Using fs to process a file
  ...
}

...

If in the above code, in the constructor the value

FileOptions.Asynchronous

replace with code

FileOptions.Asynchronous | FileOptions.DeleteOnClose

then after creation and use, the file named file.bin will be deleted.

In the same way, you can specify other additional file control options from the FileOptions enumeration.

 

2.2. Opening a file for writing

To open a file for writing with random access, the following code can be used:

...

// Opening a file for writing
using (FileStream fs = new FileStream("file1.bin", FileMode.OpenOrCreate,
FileAccess.Write, FileShare.Write, 2048, FileOptions.RandomAccess))
{
  ...
}

...

The above snippet creates a file named file1.bin. The open mode is set equal to OpenOrCreate (open a previously created file or create a new file). The access mode is set equal to FileAccess.Write. The FileShare.Write constant indicates that the file can be a shared resource for writing from various processes (threads). The buffer size is 2048 bytes. The FileOptions constant indicates that the file is being accessed randomly.

 

2.3. Opening a file for reading. An example of a byte-by-byte asynchronous file reading

To open a file for reading using constants from the FileOptions enumeration, you can use, for example, the following code

...

// Opening a file for reading
using (FileStream fs = new FileStream("file3.bin", FileMode.Open,
FileAccess.Read, FileShare.Read, 2048, FileOptions.Asynchronous))
{
  // Read bytes from file until end of file is reached
  // and write this data to array A

  // Allocate memory for buffer
  byte[] A = new byte[fs.Length];

  // additional variables
  int t, i;

  // The loop of byte data reading
  i = 0;
  do
  {
    t = fs.ReadByte(); // read the byte
    if (t != -1)
        A[i++] = (byte)t;
  }
  while (t != -1);
}

...

The previous example reads file3.bin byte by byte. The file is open for asynchronous reading. The read data is copied to byte array A.

 


Related topics