1. File System Intro Flashcards
What should you check to see before creating a new directory via code?
Whether or not it exists already.
How do you check whether a directory exists?
By passing its path to the Exists
static method of the Directory
class.
eg. Directory.Exists(“/some/path”);
How do you create a folder in C#?
By using the static CreateDirectory
method of the Directory
class and passing it the path to of the directory to be created.
eg. Directory.CreateDirectory(“/some/path”);
How do you delete a folder in C#
By using the static Delete
method of the Directory
class and passing it the path of the directory to be deleted.
eg. Directory.Delete(“/some/path”);
What happens if you try to delete a folder that has folders or files in it?
You get a System.IO.IOException
saying “Directory not empty”.
What’s the optional second argument of the Directory.Delete
method and what does it determine?
A boolean value named recursive
that determines whether the contents of the folder should also be deleted if there is any.
How do you delete a folder and all its contents if there are any in C#?
By using the Directory.Delete
method and passing the folder path and true
respectively as arguments.
How do you move a folder and all its contents into a new folder?
By using the Directory.Move
method and passing the existing directory path and the new path respectively as arguments.