File Objects in C#

CsharpProgrammingServer Side Programming

To create a new file in C#, use the FileStream object.

The following is the syntax −

FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>,
<FileAccess Enumerator>, <FileShare Enumerator>);

Let us see an example for a file “test.dat”, which is created/ opened using File object −

FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,FileAccess.ReadWrite);

The following is an example −

Example

using System;
using System.IO;

namespace FileIOApplication {
   class Program {
      static void Main(string[] args) {
         FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,
         FileAccess.ReadWrite);

         for (int i = 1; i <= 20; i++) {
            F.WriteByte((byte)i);
         }
         F.Position = 0;
         for (int i = 0; i <= 20; i++) {
            Console.Write(F.ReadByte() + " ");
         }
         F.Close();
         Console.ReadKey();
      }
   }
}
raja
Published on 31-Aug-2018 09:38:50
Advertisements