[go: up one dir, main page]

0% found this document useful (0 votes)
11 views4 pages

110 - File Handling

The document provides an overview of file handling in C# as part of a course on Net Centric Computing. It covers key concepts such as file modes, file access, and the use of StreamWriter and StreamReader classes for writing and reading files, respectively. Additionally, it discusses the DirectoryInfo class for managing directories and its various methods.

Uploaded by

jpokhrl22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

110 - File Handling

The document provides an overview of file handling in C# as part of a course on Net Centric Computing. It covers key concepts such as file modes, file access, and the use of StreamWriter and StreamReader classes for writing and reading files, respectively. Additionally, it discusses the DirectoryInfo class for managing directories and its various methods.

Uploaded by

jpokhrl22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BSc CSIT CSC367 : Prem Prasad Shrestha

NET Centric Computing


CSC367

Unit 1: Language Preliminaries

File Handling in C#
Hard Disk RAM
Non-Volatile Memory Volatile Memory
Secondary Storage Primary Storage

C# Program C# Program

Variable Variable
Methods Methods
Functions Functions
etc etc

CSC367 Language Preliminaries Slide 2

1
BSc CSIT CSC367 : Prem Prasad Shrestha

File Handling in C#
Example
public static void Main(string[] args)
{
string path = "/CSIT_NET/Data.txt";
//Read from the file
string printFile =File.ReadAllText(path);
Console.WriteLine(printFile);
//Write to the file
File.WriteAllText(path,”Text here”);
Console.ReadKey();
}

CSC367 Language Preliminaries Slide 3

File Handling in C#
File Mode
o Create – Create new file every time
o CreateNew – Create new if file is not existed.
o Open – Open the file to write
o OpenorCreate – Open file if exists or Create if not exist.
o Append – Add text every time while running.
o Truncate – Delete file
File Access
o Read
o Write
o ReadWrite

CSC367 Language Preliminaries Slide 4

2
BSc CSIT CSC367 : Prem Prasad Shrestha

File Handling in C#
Stream Writer
o StreamWriter class can be used to write text files
o StreamWriter class in C# writes characters to a stream in a specified
encoding,
o Stream.Writer.Write() method is responsible for writing to a stream,
o StreamWriter class inherited from TextWriter class that provides
methods to write an object to a string, write strings to a file
o The StreamWriter class has several constructors and provides us
with many methods,

CSC367 Language Preliminaries Slide 5

File Handling in C#
Stream Reader
o StreamReader class is used to read string from the stream.
o StreamReader reads text files,
o The namespace for it is System.IO namespace
o Mode is in Open, Access in Read or Read/Write
o It inherits TextReader class,
o It provides ReadLine() method to read data from the stream.
o If we want to read whole text file we can use ReadToEnd() method,

CSC367 Language Preliminaries Slide 6

3
BSc CSIT CSC367 : Prem Prasad Shrestha

File Handling in C#
DirectoryInfo Class
o Is used to create, delete and move directory
o It provides methods to perform operations related to directory and
subdirectory, it is a sealed class so we cannot inherit it
o C# DirectoryInfo class provides functionality to work with folders
o Declared under System.IO.
o Shares almost all of the same properties as FileInfo class.

CSC367 Language Preliminaries Slide 7

File Handling in C#
DirectoryInfo Class
o Create – used to create the new directory.
o CreateSubdirectory – used to create a subdirectory on the specified
path.
o MoveTo – used to moves DirectoryInfo instance and its
o Delete – used to delete folder or subfolder but by default it delete
empty folder. Set True as argument for delete with sub folder.
o GetDirectories – used to get the sub directories in the given folder
o GetFiles – used to get the files in the specified folder.
Lambda Expression

CSC367 Language Preliminaries Slide 8

You might also like