0% found this document useful (0 votes)
13 views33 pages

Slot 12-Working With Files and System - Io

The document provides an overview of the System.IO namespace in .NET, detailing classes such as File, FileInfo, Directory, and DirectoryInfo, which are used for file and directory manipulation. It includes demonstrations of various functionalities, including reading and writing files using StreamReader, StreamWriter, BinaryReader, and BinaryWriter. Key methods and properties of these classes are highlighted to facilitate file handling in C#.

Uploaded by

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

Slot 12-Working With Files and System - Io

The document provides an overview of the System.IO namespace in .NET, detailing classes such as File, FileInfo, Directory, and DirectoryInfo, which are used for file and directory manipulation. It includes demonstrations of various functionalities, including reading and writing files using StreamReader, StreamWriter, BinaryReader, and BinaryWriter. Key methods and properties of these classes are highlighted to facilitate file handling in C#.

Uploaded by

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

Working with Files and System.

IO
Objectives

Overview System.IO

Explain about File and FileInfo class

Explain about Directory and DirectoryInfo class

Explain and Demo about FileStream class

Demo about File and FileInfo class

Demo about Directory and DirectoryInfo class

Demo create Text file using StreamReader and StreamWriter

Demo create Binary file using BinaryWriter and BinaryReader

04/22/2025 2
Understanding the Files

A file is a collection of bytes stored on a secondary storage device, which is
generally a disk of some kind

A memory location that has a name

File can be used as an extra memory to store a large amount of data
temporarily or permanently

Each file ends with a marking-end-character or a number of bytes

04/22/2025 3
Understanding the Files

Stream: a chunk of data (sequence of bytes) containing information being
passed through to store in memory storage

04/22/2025 4
Exploring the System.IO Namespace


In the .NET, the System.IO namespace is the region of the base class libraries
devoted to file-based (and memory-based) input and output (I/O) services


System.IO defines a set of classes, interfaces, enumerations, structures, and
delegates, most of which we can find in mscorlib.dll


The types contained within mscorlib.dll, the System.dll assembly defines
additional members of the System.IO namespace

04/22/2025 5
Exploring the System.IO Namespace
Class Type Description

BinaryReader These types allow to store and retrieve primitive data types (integers,
BinaryWriter Booleans, strings, and whatnot) as a binary value
BufferedStream This type provides temporary storage for a stream of bytes that may be
committed to storage at a later time
Directory Use these classes to manipulate a machine’s directory structure. The
DirectoryInfo Directory type exposes functionality using static members, while the
DirectoryInfo type exposes similar functionality from a valid object reference
DriveInfo This class provides detailed information regarding the drives that a given
machine uses
File Use these classes to manipulate a machine’s set of files. The File type
FileInfo exposes functionality using static members, while the FileInfo type exposes
similar functionality from a valid object reference

04/22/2025 6
Exploring the System.IO Namespace
Class Type Description
FileStream This class gives you random file access (e.g., seeking capabilities) with data
represented as a stream of bytes
FileSystemWatcher This class allows you to monitor the modification of external files in a specified
directory
MemoryStream This class provides random access to streamed data stored in memory rather
than in a physical file
Path This class performs operations on System.String types that contain file or
directory path information in a platform-neutral manner
StreamWriter You use these classes to store (and retrieve) textual information to (or from) a
StreamReader file. These types do not support random file access
StringWriter Like the StreamReader/StreamWriter classes, these classes also work with
StringReader textual information. However, the underlying storage is a string buffer rather
than a physical file

04/22/2025 7
Working with Files and Directories

C# provides the following classes to work with the File system. They can be
used to access directories, access files, open files for reading or writing,
create a new file or move existing files from one location to another, etc
Class Name Description

File File is a static class that provides different functionalities like copy,
create, move, delete, open for reading or /writing, encrypt or decrypt,
check if a file exists, append lines or text to a file’s content, get last
access time, etc.
FileInfo The FileInfo class provides the same functionality as a static File class.
We have more control on how you do read/write operations on a file by
writing code manually for reading or writing bytes from a file

04/22/2025 8
Working with Files and Directories
Class Name Description

Directory Directory is a static class that provides functionality for creating, moving,
deleting and accessing subdirectories

DirectoryInfo DirectoryInfo provides instance methods for creating, moving, deleting and
accessing subdirectories

Path Path is a static class that provides functionality such as retrieving the
extension of a file, changing the extension of a file, retrieving the absolute
physical path, and other path related functionalities

04/22/2025 9
FileStream Class Demonstration

Provides a Stream for a file, supporting both synchronous and asynchronous
read and write operations

04/22/2025 10
FileStream Class Demonstration

On Windows
OS

On Mac OS

04/22/2025 11
Working with File Class


File is a static class to read\write from physical file with less coding

Static File class provides functionalities such as create, read\write, copy,
move, delete and others for physical files

The static File class includes various utility method to interact with physical
file of any type e.g. binary, text etc. Use this static File class to perform some
quick operation on physical file

04/22/2025 12
Working with File Class

Important Methods of static File class
Method Name Description
AppendAllLines Appends lines to a file, and then closes the file. If the specified file does not exist, this
method creates a file, writes the specified lines to the file, and then closes the file

AppendAllText Opens a file, appends the specified string to the file, and then closes the file. If the file
does not exist, this method creates a file, writes the specified string to the file, then
closes the file
AppendText Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a
new file if the specified file does not exist
Copy Copies an existing file to a new file. Overwriting a file of the same name is not allowed
Create Creates or overwrites a file in the specified path
CreateText Creates or opens a file for writing UTF-8 encoded text

04/22/2025 13
Working with File Class
Method Name Description
Delete Deletes the specified file
Decrypt Decrypts a file that was encrypted by the current account using the Encrypt
method
Encrypt Encrypts a file so that only the account used to encrypt the file can decrypt it
Exists Determines whether the specified file exists
GetAccessControl Gets a FileSecurity object that encapsulates the access control list (ACL) entries
for a specified file
Move Moves a specified file to a new location, providing the option to specify a new file
name
Open Opens a FileStream on the specified path with read/write access
ReadAllBytes Opens a binary file, reads the contents of the file into a byte array, and then closes
the file

04/22/2025 14
Working with File Class
Method Name Description
ReadAllLines Opens a text file, reads all lines of the file, and then closes the file
ReadAllText Opens a text file, reads all lines of the file, and then closes the file
Replace Replaces the contents of a specified file with the contents of another file, deleting
the original file, and creating a backup of the replaced file

WriteAllBytes Creates a new file, writes the specified byte array to the file, and then closes the file.
If the target file already exists, it is overwritten

WriteAllLines Creates a new file, writes a collection of strings to the file, and then closes the file
WriteAllText Creates a new file, writes the specified string to the file, and then closes the file. If
the target file already exists, it is overwritten

04/22/2025 15
File Class Demonstration-01

04/22/2025 16
File Class Demonstration-02

04/22/2025 17
Working with FileInfo Class

The FileInfo class provides the same functionality as the static File class but we
have more control on read/write operations on files by writing code manually for
reading or writing bytes from a file

Important Properties and Methods of FileInfo class:
Property Name Description

Directory Gets an instance of the parent directory

DirectoryName Gets a string representing the directory's full path

Exists Gets a value indicating whether a file exists

04/22/2025 18
Working with FileInfo Class
Property Name Description
Extension Gets the string representing the extension part of the file

FullName Gets the full path of the directory or file

IsReadOnly Gets or sets a value that determines if the current file is read only

LastAccessTime Gets or sets the time the current file or directory was last accessed

LastWriteTime Gets or sets the time when the current file or directory was last written to

Length Gets the size, in bytes, of the current file

Name Gets the name of the file

04/22/2025 19
Working with FileInfo Class
Method Name Description
AppendText Creates a StreamWriter that appends text to the file represented by this instance
of the FileInfo
CopyTo Copies an existing file to a new file, disallowing the overwriting of an existing file
Create Creates a file
CreateText Creates a StreamWriter that writes a new text file
Decrypt Decrypts a file that was encrypted by the current account using the Encrypt
method
Delete Deletes the specified file
Encrypt Encrypts a file so that only the account used to encrypt the file can decrypt it
GetAccessControl Gets a FileSecurity object that encapsulates the access control list (ACL) entries
for a specified file

04/22/2025 20
Working with FileInfo Class
Method Name Description

MoveTo Moves a specified file to a new location, providing the option to specify a new file
name

Open Opens a in the specified FileMode

OpenRead Creates a read-only FileStream

OpenText Creates a StreamReader with UTF8 encoding that reads from an existing text file

OpenWrite Creates a write-only FileStream

Replace Replaces the contents of a specified file with the file described by the current
FileInfo object, deleting the original file, and creating a backup of the replaced file

ToString Returns a path as string

04/22/2025 21
FileInfo Class Demonstration

04/22/2025 22
Working with Directory Class

Exposes static methods for creating, moving, and enumerating through
directories and subdirectories. This class cannot be inherited

Important methods of Directory class:
Method Name Description

CreateDirectory(String) Creates all directories and subdirectories in the specified path unless
they already exist.
Delete(String) Deletes an empty directory from a specified path.

EnumerateDirectories(String) Returns an enumerable collection of directory full names in a


specified path.

04/22/2025 23
Working with Directory Class
Method Name Description
EnumerateFiles(String) Returns an enumerable collection of full file names in a specified path.

Exists(String) Determines whether the given path refers to an existing directory on


disk.
GetCurrentDirectory() Gets the current working directory of the application.

GetDirectories(String) Returns the names of subdirectories (including their paths) in the


specified directory.
GetFiles(String) Returns the names of files (including their paths) in the specified
directory.
GetParent(String) Retrieves the parent directory of the specified path, including both
absolute and relative paths.
Move(String, String) Moves a file or a directory and its contents to a new location.

04/22/2025 24
Directory Class Demonstration

On Windows
OS

On Linux OS

04/22/2025 25
Working with DirectoryInfo Class

Exposes instance methods for creating, moving, and enumerating through
directories and subdirectories. This class cannot be inherited

Important Properties and Methods of DirectoryInfo class:
Property Name Description

Attributes Gets or sets the attributes for the current file or directory
CreationTime Gets or sets the creation time of the current file or directory

Exists Gets a value indicating whether the directory exists


Extension Gets the string representing the extension part of the file
FullName Gets the full path of the directory or file

04/22/2025 26
Working with DirectoryInfo Class
Property Name Description
Create() Creates a directory
CreateSubdirectory(String) Creates a subdirectory or subdirectories on the specified path. The
specified path can be relative to this instance of the DirectoryInfo class
Delete() Deletes this DirectoryInfo if it is empty
GetDirectories(String) Returns an array of directories in the current DirectoryInfo matching the
given search criteria
GetFiles(String) Returns a file list from the current directory matching the given search
pattern
MoveTo(String) Moves a DirectoryInfo instance and its contents to a new path
GetFileSystemInfos(String) Retrieves an array of strongly typed FileSystemInfo objects representing
the files and subdirectories that match the specified search criteria

04/22/2025 27
Working with DirectoryInfo Class Demonstration

04/22/2025 28
Working with StreamWriter and StreamReader

StreamReader: StreamReader is a helper class for reading characters from a
Stream by converting bytes into characters using an encoded value. It can be
used to read strings (characters) from different Streams like FileStream,
MemoryStream, etc

StreamWriter: StreamWriter is a helper class for writing a string to a Stream by
converting characters into bytes. It can be used to write strings to different
Streams such as FileStream, MemoryStream, etc

04/22/2025 29
StreamWriter and StreamReader Demonstration

04/22/2025 30
Working with BinaryWriter and BinaryReader

BinaryReader and BinaryWriter allow to read and write discrete data types
to an underlying stream in a compact binary format

04/22/2025 31
BinaryWriter and BinaryReader Demonstration

04/22/2025 32
Summary

Concepts were introduced:
 Overview about System.IO
 Explain and Demo about FileStream class
 Explain about File and FileInfo class
 Explain about Directory and Directory Info class
 Demo about File and FileInfo class
 Demo about Directory and DirectoryInfo class
 Demo create Text file using StreamReader and StreamWriter
 Demo create Binary file using BinaryWriter and BinaryReader
33

You might also like