 Hello everyone and welcome to today's session. Today we'll see two classes file class and directory class in C sharp.net. At the end of this session student will be able to implement C sharp.net programs to perform various operations to files and directories using file and directory classes. We will begin with file class. Use the file class for performing operations such as copying, moving, renaming, creating, opening, deleting and appending to a single file at a time. We can also use the file class to get and set file attributes or datetime information related to the creation, access and writing to a file. All file methods require the path to the file that we are manipulating. The static methods of the file class perform security checks on all methods. In members that accept a path as an input string, the path must be well formed, else exception will be raised. The path can refer to a file or directory. Here we'll see commonly used methods of a file class. The first method is append text. It accepts path. If the file specified by path does not exist, it is created. If the file does exist, it creates a stream writer that appends UTF-8 encoded text to an existing file and returns it. Copy method accepts two parameters, source file name and destination file name. It copies an existing file to a new file. Overwriting a file of the same name is not allowed. Destination file name cannot be a directory or an existing file. If the destination file exists, IO exception is thrown. Next method is create. It accepts single parameter that is path. It creates or overwrites a file at the specified path. Create text method creates or opens a file for writing UTF-8 encoded text. If the file already exists, its contents are overwritten. Delete method deletes the specified file. Next is exists method. It returns Boolean. It determines whether the specified file exists. Next method is get creation time. The method returns the creation date and time of the specified file or directory. Next method is get last access time. Path is passed as a parameter. This method returns date and time when the specified file or directory was last accessed. Next method is get last write time. This method returns date and time when the specified file or directory was last written to. Move method accepts two parameters, source file name and destination file name. It moves a specified file to a new location. Next is open method. It opens a file stream on the specified path with read or write access with no sharing. Next method is open read. This method opens an existing file for reading. Next method is open write. This method opens an existing file or creates a new file for writing. Next method is read all bytes method. It opens a binary file, reads the contents of the file into a byte array and then closes the file. Returns a byte array containing the contents of the file. Next method is read all lines method. It opens a text file, reads all lines of the file into a string array and then closes the file. Write all bytes method, 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. Next method is write all lines. This method creates a new file, writes the specified string array to the file and then closes the file. Now let's see a program with file class. In this program we have used file class. So we have included system.io namespace. This path is a string variable. It stores the path of mytest.txt file. This if block checks whether file exists. If it doesn't exist, this block will be executed. These statements will be executed. So here we have string array. String array has the values as hello and welcome. Then we have used write all lines method of file class. Here we have three parameters. First parameter is path. Then the next one is string array contains and third is encoding. So we have used path, this path. Then we have string array that is create text. This will be written to the file with this encoding, utf8. But if the file already exists, these statements will not be executed. Then we have string variable append text. This has a string. This is extra text plus new line character. Then we have used append all text method of file class. This has three parameters. First is path. Then the other is string contains and third is encoding. So we have used path, then this is string variable and encoding utf8. So this will also be written to the file. This is extra text plus new line. Then we are reading from the file. We have string array read text. Here read all lines method of file class is used. It has two parameters. First is path and the other is encoding. And after reading the contents of the file will be stored in read text string array. And here we are printing s from read text. Now let's execute this program. The text printed is hello and welcome. This is extra text. If I execute it once again, only the last statement, this is extra text is appended because these statements will not be executed if the file already exists. And after that it will be this text. This is extra text will be appended every time. So if I execute it once again, we will have one more statement that is this is extra text. Let's see where the file exists. This is my test.txt. These are the file contents. Now pause the video and find the answer. How to check if the file exists or not using file class? Write the syntax and example. Cross check your answer with the syntax and example given here. Exists method returns boolean. If return value is true, if block will be executed. Next class is directory class. It has static methods for creating, moving and enumerating through directories and subdirectories. This class cannot be inherited. Now we'll see the methods of directory class. First method is create directory. This method creates all directories and subdirectories in the specified path unless they already exist. Delete method deletes a specified directory and optionally any subdirectories. It deletes an empty directory from a specified path. IO exception is thrown if the directory specified by path is not empty. Next method is exists method. It determines whether the given path refers to an existing directory on disk. Next method is get directories method. It returns the names of subdirectories in the specified directory. Next method is get files method. It returns the names of the files in the specified directory. Next is get creation time. This method returns the creation date and time of a directory. Next method is get last access time. This method returns Once the date and time, the specified file or directory was last accessed. Next method is getLastWriteTime. This method returns the date and time the specified file or directory was last written to. Move method accepts two parameters, source directory name and destination directory name. It moves a file or directory and its contents to a new location. Now let's see a program with directory class. In this program we have used directory class. And for using this directory class, we need to include System.ioNamespace. This is the source directory and this is the destination directory. Next method is getList() This is the source directory and this is the directory class. System.ioNamespace. This is the source directory and this is the destination directory. First we'll see the directory source. Source directory has myTest.txt file and the destination directory doesn't exist. And here we have used move method of directory class. Move method accepts two parameters that is source directory name and destination directory name. When I execute this program, all the files from source directory are copied to the destination directory. And if this move is not successful, exception will be thrown. If the source directory doesn't exist, then the exception will be thrown. I'll execute this program. Now instead of source directory, here I have destination directory which is created through program. And the file from source directory, it was myTest.txt. It is copied to destination directory. In this session we have seen file and directory classes with example. Thank you.