FileOptions Enumeration
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.IOAssembly: mscorlib (in mscorlib.dll)
| Member name | Description | |
|---|---|---|
| Asynchronous | Indicates that a file can be used for asynchronous reading and writing. | |
| DeleteOnClose | Indicates that a file is automatically deleted when it is no longer in use. | |
| Encrypted | Indicates that a file is encrypted and can be decrypted only by using the same user account used for encryption. | |
| None | Indicates no additional parameters. | |
| RandomAccess | Indicates that the file is accessed randomly. The system can use this as a hint to optimize file caching. | |
| SequentialScan | Indicates 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. |
|
| WriteThrough | Indicates that the system should write through any intermediate cache and go directly to disk. |
The following code example writes data to a file and then reads the data using the FileStream object.
using System; using System.IO; using System.Text; using System.Security.AccessControl; namespace FileSystemExample { class FileStreamExample { public static void Main() { try { // Create a file and write data to it. // Create an array of bytes. byte[] messageByte = Encoding.ASCII.GetBytes("Here is some data."); // Specify an access control list (ACL) FileSecurity fs = new FileSecurity(); fs.AddAccessRule(new FileSystemAccessRule(@"DOMAINNAME\AccountName", FileSystemRights.ReadData, AccessControlType.Allow)); // Create a file using the FileStream class. FileStream fWrite = new FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs); // Write the number of bytes to the file. fWrite.WriteByte((byte)messageByte.Length); // Write the bytes to the file. fWrite.Write(messageByte, 0, messageByte.Length); // Close the stream. fWrite.Close(); // Open a file and read the number of bytes. FileStream fRead = new 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. byte[] readBytes = new byte[length]; // Read the data from the file. fRead.Read(readBytes, 0, readBytes.Length); // Close the stream. fRead.Close(); // Display the data. Console.WriteLine(Encoding.ASCII.GetString(readBytes)); Console.WriteLine("Done writing and reading data."); } catch (Exception e) { Console.WriteLine(e); } Console.ReadLine(); } } }
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.I tried to create the PowerShell to be a match to the C# sample. However, I found an issue with the permisisons when I tried it.
As you will note from the PowerShell Sample, I set the file system rights for the file creation to "[system.Security.AccessControl.FileSystemrights]::FullControl".
In the C# sample, this is set to "FileSystemRights.ReadData" (in PowerShell [System.Security.AccessControl.FileSystemRights]::ReadData). With this setting the file open later in the PowerShell sample fails.
- 4/23/2007
- Thomas Lee
- 4/6/2008
- Thomas Lee
# get-encoding.ps1
# Writes data to a file and then reads the data using the FileStream object
# Thomas Lee
# MSDN Sample recoded using PowerShell
# Create a file
[byte[]] $messageByte = [system.text.Encoding]::ascii.getbytes("Here is some data.")
$fs = new-object system.security.accesscontrol.FileSecurity
# Create a new access rule
$user="UK0N121\tfl"
$rights=[system.security.accesscontrol.filesystemrights]::FullControl
$access=[system.security.accesscontrol.accesscontrolType]::Allow
$rule = new-object system.security.accesscontrol.FileSystemAccessRule $user, $rights, $Access
# Add the rule to the file object
$fs.AddAccessRule($rule)
# Create a file
$fn = "d:\foo\test.txt"
$mode = [system.IO.filemode]::Create
$fsr = [system.Security.AccessControl.FileSystemrights]::FullControl
$shr = [system.Io.FileShare]::None
$fopt = [system.Io.FileOptions]::None
$fwrite = new-object system.io.filestream $fn, $mode, $fsr, $shr, 8,$fopt, $fs
# Write byes to the numberf of bytes to the file
$fWrite.WriteByte([byte] $messageByte.Length)
# Write bytes to the file
$fWrite.Write($messageByte, 0, $messageByte.Length);
# Close the stream
$fwrite.close()
# Now open the file and read lenght and bytes
$fn = "d:\foo\test.txt"
$mode =[System.IO.FileMode]::Open
$fRead = new-object System.IO.FileStream $fn, $mode
# Get The first byte - it is the string length.
$length = [int] $fRead.ReadByte()
# Create a new byte array for the data.
[byte[]] $readBytes = new-object byte[] $length
# Read the data from the file.
$get=$fRead.Read($readBytes, 0, $readBytes.Length)
# Close the stream.
$fRead.Close()
# Display the data.
"Length: {0}" -f $length
"String: {0}" -f [system.text.Encoding]::ASCII.getString($readBytes)
# Done
"Done writing and reading data."
This script produces the following output:
PSH [D:\foo]: .\get-encoding.ps1
Length: 18
String: Here is some data.
Done writing and reading data.
- 4/23/2007
- Thomas Lee
- 4/23/2007
- Thomas Lee