C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: With FileMode.Create, we create a new file (or create it again if it already exists). With FileMode.Open, we open an existing file.
UsingC# program that uses File.Open
using System;
using System.IO;
class Program
{
static void Main()
{
using (FileStream stream = File.Open("C:\\bin", FileMode.Create))
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(303);
writer.Write(720);
}
using (FileStream stream = File.Open("C:\\bin", FileMode.Open))
using (BinaryReader reader = new BinaryReader(stream))
{
int a = reader.ReadInt32();
int b = reader.ReadInt32();
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}
Output
303
720
Enum values:
FileMode.Append
FileMode.Create
FileMode.CreateNew
FileMode.Open
FileMode.OpenOrCreate
FileMode.Truncate