We know that File.Open method is used to open a file (i.e. creating a stream) to read/write the file. Alos, the non-static FileInfo.Open methods can be used to read/write a file. FileInfo.Open has following overloads:
- public FileStream Open(FileMode mode, FileAccess access);
- public FileStream Open(FileStreamOptions options);
- public FileStream Open(FileMode mode, FileAccess access, FileShare share);
- public FileStream Open(FileMode mode);
- public FileStream OpenRead();
- public StreamReader OpenText();
FileMode enumerations:
- CreateNew = 1,
- Create = 2,
- Open = 3,
- OpenOrCreate = 4,
- Truncate = 5,
- Append = 6
FileAccess enumerations:
- Read = 1,
- Write = 2,
- ReadWrite = 3
Look at the following table to see different File Modes:
| File Mode | Meaning |
| CreateNew = 1 | Throws error if file exists |
| Create = 2 | Overwrites if file exists |
| Open = 3 | Throws error if file not found |
| OpenOrCreate = 4 | Creates file if file not found |
| Truncate = 5 | Opens an existing file and erase all of its contents immediately (set length to 0). |
| Append = 6 | Opens file if it exists and seeks to the end of the file or creates a new file. |
Look at the following table to see different File Access:
| File Access | Meaning |
| Read | Can Read from a file |
| Write | Can Write into a file |
| ReadWrite | Can Read and Write a file |
Example. Create mode
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
// File will be overwritten if it already exists
using FileStream fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write);
// Write "Hello World!" to the file
fileStream.Write(System.Text.Encoding.UTF8.GetBytes("Hello World!"), 0, "Hello World!".Length);
Console.WriteLine($"The {target_file} is created successfully.");
}
Example. CreateNew mode
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
// Exception will be thrown if it already exists
using FileStream fileStream = fileInfo.Open(FileMode.CreateNew, FileAccess.Write);
// Write "Hello World!" to the file
fileStream.Write(System.Text.Encoding.UTF8.GetBytes("Hello World!"), 0, "Hello World!".Length);
Console.WriteLine($"The {target_file} is created successfully.");
}
Example. Open mode
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
string message = "Hello World!";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
// Exception will be thrown if file is missing
using FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Write);
// Write "Hello World!" to the file
fileStream.Write(System.Text.Encoding.UTF8.GetBytes(message), 0, message.Length);
Console.WriteLine($"The {target_file} is written successfully.");
}
Example. OpenOrCreate mode
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
string message = "Hello World!";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
// a new file will be created if file is missing
using FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write);
// Write "Hello World!" to the file
fileStream.Write(System.Text.Encoding.UTF8.GetBytes(message), 0, message.Length);
Console.WriteLine($"The {target_file} is written successfully.");
}
Example. Append mode
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
string message = "Hello World!";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
// Exception will be thrown if file is missing
using FileStream fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write);
// Append "Hello World!" to the file
fileStream.Write(System.Text.Encoding.UTF8.GetBytes(message), 0, message.Length);
Console.WriteLine($"The {target_file} is written successfully.");
}
Notes. - Change the FieAccess.Write to FileAccess.Read to perform Read operation on file.
- FieAccess mode is optional. You can even use Open(FileMode mode) to create FileStream.
- To read a text file, you can use OpenText method which returns StreamReader.
Example. OpenText method
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source_folder = Path.Combine(desktop, "source_folder");
string target_file = "sample.txt";
if (Directory.Exists(source_folder))
{
FileInfo fileInfo = new FileInfo(Path.Combine(source_folder, target_file));
using StreamReader reader = fileInfo.OpenText();
string allData = reader.ReadToEnd();
Console.WriteLine($"The {target_file} contains:\n {allData}");
}
No comments:
Post a Comment