TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# File.Open Examples

Use the File.Open method with the FileStream and BinaryReader types.
File.Open. How can you use File.Open? Found in the System.IO namsespace, File.Open returns a FileStream that you can use. The arguments you pass to it determine its action. It is often used within a using-statement.File
Example. Here we use File.Open as part of the using-resource-acquisition pattern. This ensures proper recovery of system resources. The first argument is the name of the file we are acting upon. The second is a FileMode enumerated constant.

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.

Using
C# 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
BinaryWriter and BinaryReader are a practical use of FileStream. These constructors receive Stream references. Because File.Open returns a FileStream, and FileStream is derived from Stream, we can use FileStream in the constructors.StreamBinaryWriterBinaryReader
Round-tripping. We can see in this program that the numbers 303 and 720 are written to the "C:\bin" file. Then, they are read back into integers from that same file. This is an inefficient way to write two numbers to the screen.
FileMode. Let's review the FileMode enumerated type. To use FileMode, type FileMode and press the period key, and then read the descriptions for each constant in Visual Studio. The constants are fairly self-descriptive.
Enum values: FileMode.Append FileMode.Create FileMode.CreateNew FileMode.Open FileMode.OpenOrCreate FileMode.Truncate
Alternatives. There are alternative methods to File.Open. You can instead call File.OpenRead, File.OpenText, and File.OpenWrite. Conceptually, these methods are the same as using File.Open with certain FileMode arguments.
Summary. We used the File.Open method and described the FileMode enumerated type. With the using statement, and the implicit casting in the C# language, we can use File.Open as part of a powerful file-handling construction.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf