Deleting a file 

In this operation, we delete a file present in the specified location:

File.Delete(sourceFileLocation);

When the code is executed, we will see that the file is deleted from the source file location. Once the code is executed, we will see that the file specified in the sourceFileLocation path has been deleted.

Please note that operations that work with the File class work the same way with the FileInfo class. The same implementations that we have done with the File class can be done via the FileInfo class as well.

In all the preceding examples, we have been hard-coding the path property of the file. This is not a recommended practice as it is error-prone. For example, if you look at the actual path of any file and compare it to what we need to supply in the program, you will notice a difference:

  • Actual path: C:File LocationChapter 20Sample.txt
  • Path which we need to specify in the program: C:\File Location\Chapter 20\Sample.txt

We need to provide some extra slashes in the path. Also, when we are combining the folder path with the file path, we need to concatenate them with an extra  as well. For these reasons, hardcoding the path is not a recommended practice. A better approach is to use the Path helper class. The following code shows how to use it:

string sourceFileLocation = @"C:UCN Code BaseProgramming-in-C-Exam-70-483-MCSD-GuideBook70483SamplesChapter 20";
string fileName = @"IO Operations.txt";
string properFilePath = Path.Combine(sourceFileLocation, fileName);
Console.WriteLine(Path.GetDirectoryName(properFilePath));
Console.WriteLine(Path.GetExtension(properFilePath));
Console.WriteLine(Path.GetFileName(properFilePath));
Console.WriteLine(Path.GetPathRoot(properFilePath));

In the preceding code implementation, we have declared a folder path. Please also note the use of & before the filename. This is an escape character that allows us to not specify an extra  in the folder path structure.  We have also declared a filename and are now combining the two together using the helper static class: Path. Once we have combined them, we retrieve the properties in the resulting file path. If the code is executed, we get the following output:

Let's examine the output:

  • Path.GetDirectoryName: This returns the directory name of the combined path file. Note that it has the complete absolute directory path.
  • Path.GetExtension: This returns the file extension of the combined path file. In this case, it's a .txt file.
  • Path.GetFileName: This returns the name of the file. 
  • Path.GetPathRoot: This returns the root of the filepath. In this case, it's C:, hence it's mentioned in the output.

Now that we are aware of basic operations on files, we will look at how to access and modify the contents of a file. For this, we will look at the operations available in FileStream.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.128.145