Home >Backend Development >C#.Net Tutorial >What are file operations in C#?

What are file operations in C#?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-09-09 08:01:06635browse

What are file operations in C#?

C# has the following file operations -

  • Create, open, read and write files.
  • Append,
  • Delete, etc.

The FileStream class in the System.IO namespace facilitates reading, writing, and closing files. This class is derived from the abstract class Stream.

You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows -

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

This also includes file operations, as shown below -

FileMode The enumerator defines various methods for opening files. The members of the FileMode enumerator are -

  • Append - It opens an existing file and places the cursor at the end of the file, or creates the file if it does not exist document.

  • Create - Creates a new file.

  • CreateNew - It specifies that the operating system should create a new file.

  • Open − It opens an existing file.

  • OpenOrCreate − It specifies to the operating system that the file should be opened if it exists, otherwise a new file should be created.

  • Truncate - It opens an existing file and truncates its size to zero bytes.

  • ul>

    FileAccess - FileAccess enumerator has members -

    • Read,
    • ReadWrite and
    • Write.

    FileShare - The FileShare enumerator has the following members -

      Inheritable - It allows file handles Pass inheritance to child process

    • None - it refuses to share the current file

    • for reading - It allows the file to be opened for reading.

    • ReadWrite - It allows opening a file for reading and writing

    • Write - It allows opening a file for writing

    Let's see an example of creating, opening and reading the contents of a file -

    Example

    Live Demonstration p>

    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();
          }
       }
    }

    Output

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

The above is the detailed content of What are file operations in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:File permissions in C#Next article:File permissions in C#