What are file operations in C#?

WBOY
Release: 2023-09-09 08:01:06
forward
461 people have browsed it

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( , , , );
Copy after login

This also includes file operations, as shown below -

FileModeThe 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 -

      < li>

      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< /p>

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

    Example

    Live Demonstration

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

    Output

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

    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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!