Your applications will often need to perform input and output with files and directories. The System.IO
namespace contains classes for this purpose.
Start Microsoft Visual Studio 2015. In Visual Studio, press Ctrl + Shift + N or choose File | New | Project….
In the New Project dialog, in the Installed Templates list, select Visual C#. In the center list, select Console Application, type Name as Ch10_FileSystem, change location to C:\Code
, type solution name as Chapter10, and then click on OK.
At the top of the Program.cs
file, add the following import statements:
using static System.Console; using System.IO;
In the Main
method, write the following statements to check for the existence of a directory, and then, create and delete it:
// define a directory string dir = @"C:\Code\Ch10_Example\"; // check if it exists WriteLine($"Does {dir} exist? {Directory.Exists(dir)}"); // create a directory Directory.CreateDirectory(dir); WriteLine($"Does {dir} exist...