In the last section, we have seen and read the text with simple files, we have used the file class for this, but it is much more than just reading and writing. When mixed with the directory class, we can do a lot of file system operation such as rename, shake, delete etc.
This section will give several examples of doing those things. The explanations will be limited, because the methods used are very simple and easy to use. You should be aware of two things: First of all, make sure you system in this way. IO namespace imports:
Using System.IO;
Also, be aware that we are not managing any exceptions here, we will see that files and directories exist before using, but there are no exceptions, so if anything goes wrong, then the application crashes Will be done. Exception handling is usually a good idea when doing IO operations. For more information, please read the exception handling chapter in this tutorial.
In all instances, we use simple file and directory names, which must exist in the same directory as your application's EXE file generated. In Project Settings, you can see where your EXE file has been created.
Deleting a file
if(File.Exists("test.txt"))
{
File.Delete("test.txt");
if(File.Exists("test.txt") == false)
Console.WriteLine("File deleted...");
}
else
Console.WriteLine("File test.txt does not yet exist!");
Console.ReadKey();
Deleting a directory
if(Directory.Exists("testdir"))
{
Directory.Delete("testdir");
if(Directory.Exists("testdir") == false)
Console.WriteLine("Directory deleted...");
}
else
Console.WriteLine("Directory testdir does not yet exist!");
Console.ReadKey();
If testdir is not empty, you will receive an exception. Why? Because this version of Delete() on the Directory class only works on empty directories. It's very easy to change though:
Directory.Delete("testdir", true);
Additional parameters ensure that the Delete () method is recursive, which means that it will cross the subdirectories before removing the directory and delete its file.
Renaming a file
if(File.Exists("test.txt"))
{
Console.WriteLine("Please enter a new name for this file:");
string newFilename = Console.ReadLine();
if(newFilename != String.Empty)
{
File.Move("test.txt", newFilename);
if(File.Exists(newFilename))
{
Console.WriteLine("The file was renamed to " + newFilename);
Console.ReadKey();
}
}
}
You will see that we use the Move () method to rename the file. Why not change a Rename() method? Because there is no such method, because moving and rename is essentially the same thing.
Renaming a directory
if(Directory.Exists("testdir"))
{
Console.WriteLine("Please enter a new name for this directory:");
string newDirName = Console.ReadLine();
if(newDirName != String.Empty)
{
Directory.Move("testdir", newDirName);
if(Directory.Exists(newDirName))
{
Console.WriteLine("The directory was renamed to " + newDirName);
Console.ReadKey();
}
}
}
Creating a new directory
Console.WriteLine("Please enter a name for the new directory:");
string newDirName = Console.ReadLine();
if(newDirName != String.Empty)
{
Directory.CreateDirectory(newDirName);
if(Directory.Exists(newDirName))
{
Console.WriteLine("The directory was created!");
Console.ReadKey();
}
}