This documentation is archived and is not being maintained.

FileOptions Enumeration

Represents additional options for creating a FileStream object.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public enum class FileOptions

Member nameDescription
NoneIndicates no additional parameters.
WriteThroughIndicates that the system should write through any intermediate cache and go directly to disk.
AsynchronousIndicates that a file can be used for asynchronous reading and writing.
RandomAccessIndicates that the file is accessed randomly. The system can use this as a hint to optimize file caching.
DeleteOnCloseIndicates that a file is automatically deleted when it is no longer in use.
SequentialScanIndicates that the file is to be accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed.

Specifying this flag can increase performance for applications that read large files using sequential access. Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.

EncryptedIndicates that a file is encrypted and can be decrypted only by using the same user account used for encryption.

Use the FileOptions enumeration to specify advanced file options.

The following code example writes data to a file and then reads the data using the FileStream object.

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::AccessControl;
using namespace System::Security::Principal;

int main()
        // Create a file and write data to it. 

        // Create an array of bytes. 
        array<Byte>^ messageByte =
            Encoding::ASCII->GetBytes("Here is some data.");

        // Specify an access control list (ACL)
        FileSecurity^ fs = gcnew FileSecurity();

            gcnew FileSystemAccessRule("MYDOMAIN\\MyAccount",
            FileSystemRights::Modify, AccessControlType::Allow));

        // Create a file using the FileStream class.
        FileStream^ fWrite = gcnew FileStream("test.txt",
            FileMode::Create, FileSystemRights::Modify,
            FileShare::None, 8, FileOptions::None, fs);

        // Write the number of bytes to the file.

        // Write the bytes to the file.
        fWrite->Write(messageByte, 0, messageByte->Length);

        // Close the stream.

        // Open a file and read the number of bytes.

        FileStream^ fRead = 
            gcnew FileStream("test.txt", FileMode::Open);

        // The first byte is the string length. 
        int length = (int)fRead->ReadByte();

        // Create a new byte array for the data. 
        array<Byte>^ readBytes = gcnew array<Byte>(length);

        // Read the data from the file.
        fRead->Read(readBytes, 0, readBytes->Length);

        // Close the stream.

        // Display the data.

        Console::WriteLine("Done writing and reading data.");

    catch (IdentityNotMappedException^)
        Console::WriteLine("You need to use your own credentials " +
            " 'MYDOMAIN\\MyAccount'.");

    catch (IOException^ ex)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0