Tables 11-1, 11-2, and 11-3 show file mode, file access, and file share information.
|
Mode |
Description |
|---|---|
|
Append |
Opens the file if it exists and seeks to the end of the file, or creates a new file. |
|
Create |
Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. |
|
CreateNew |
Specifies that the operating system should create a new file. |
|
Open |
Specifies that the operating system should open an existing file. |
|
OpenOrCreate |
Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. |
|
Truncate |
Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes. |
|
Access |
Description |
|---|---|
|
Read |
Read access to the file. Data can be read from the file. |
|
ReadWrite |
Read and write access to the file. Data can be written to and read from the file. |
|
Write |
Write access to the file. Data can be written to the file. |
|
Share |
Description |
|---|---|
|
Inheritable |
Makes the file handle inheritable by child processes. |
|
None |
Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed. |
|
Read |
Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, if this flag is specified, additional permissions might still be needed to access the file. |
|
ReadWrite |
Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for writing or reading (by this process or another process) will fail until the file is closed. However, if this flag is specified, additional permissions might still be needed to access the file. |
|
Write |
Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, if this flag is specified, additional permissions might still be needed to access the file. |
There are many ways to create a file. The two most common classes are File and FileInfo. Both classes provide a means to create, copy, delete, move, and open files. However, the FileInfo class is preferred because it eliminates redundant security checks when reusing an object. The FileSystemWatcher class provides a means to receive notification when the file system is modified with specific interest in attribute, size, last write, or other changes.
The following examples demonstrate creating, opening, deleting, moving, and copying files, as well as using the FileSystemWatcher class.
using System; using System.IO; namespace Client.Chapter_11___Files_and_Streams { public class MovingAFile { static void Main(string[] args) { FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt"); MyFile.Create(); MyFile.MoveTo(@"c:\Projects\MyFolder\Moved Testing.txt"); MyFile.MoveTo(@"C:|projects\MyFolder"); } } }
using System; using System.IO; namespace Client.Chapter_11___Files_and_Streams { public class CopyingAFile { static void Main(string[] args) { FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt"); MyFile.Create(); MyFile.CopyTo(@"c:\Projects\MyFolder\Moved Testing.txt"); //Or MyFile.CopyTo( @"c:\Projects\MyFolder\Moved Testing.txt", true); } } }
using System; using System.IO; namespace Client.Chapter_11___Files_and_Streams { public class Test { public static void Main(string[] args) { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = @"c:\Test"; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = "*.txt"; watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); watcher.EnableRaisingEvents = true; } public static void OnChanged(object source, FileSystemEventArgs e) { Console.WriteLine("Event Fired"); } public static void OnRenamed(object source, RenamedEventArgs e) { Console.WriteLine("Event Fired"); } } }