Introduction
Basic manipulation and navigating files & directories within the Windows Operating System which uses the “File Explorer” is really quite easy for most users. However; in some instances, file management could be an important part of an enterprise application. As a result, the Microsoft .NET framework provides a significant class-libraries inside System.IO namespace for file handling. Which returns the favor to developers, to do certain basic and advanced manipulation of files and directories.
If you are a newbie and need to somehow learn: how to handle and manage some file/directory manipulation within his/her application, this article is made just for you. However; those who are more advanced developers and/or readers are also welcome.
Let’s get started then.
In this article, we are going to explore the following classes: System.IO.Path, System.IO.Directory & System.IO.DirectoryInfo, System.IO.File to see the features it has to offer. See our objectives below.
Objectives
- Using the Path class to deal with filenames, directories, and paths to manage file-extensions, temporary files, temporary path, directory-name, and file-name, etc.
- Using the Directory and DirectoryInfo classes to manipulate directories (create and delete) and add a directory to a certain access-rule.
- Using the File class to deal with read and write to file.
Project Structure
The following classes, Explore_File_IO_Path.cs, Explore_File_IO_Directory.cs, Explore_File_IO_File.cs are unit test classes. The objective of these unit tests is basically to test the methods that we are going to tackle and study. Moreover; there’s one common thing about these classes, they have an Initialize method which literally initializes the projectPathLocation to get the directory of the running project. See the source code below.
- string projectPathLocation = string.Empty;
- [TestInitialize]
- public void Initialize()
- {
- projectPathLocation = AppConfig.ProjectPathLocation;
- }
The AppConfig.cs file acts as a configuration-class to get those project paths and directory structure in mind. See the source code below.
- public static class AppConfig
- {
- public const string ProjectFiles = "project-files";
- public static string[] ProjectDirectories = new string[]
- {
- "App-Data-1",
- "App-Data-2",
- "App-Data-3"
- };
- public static Dictionary<string, string[]> projectDirectoriesWithChild = new Dictionary<string, string[]>
- {
- { "Directory-1",
- new string[] { "A", "B", "C", "D", "E" }},
- { "Directory-2",
- new string[] { "A", "B", "C", "D", "E", }},
- { "Directory-3",
- new string[] { "A", "B", "C", "D", "E", }},
- { "Directory-4",
- new string[] { "A", "B", "C", "D", "E", }}
- };
- public static string ProjectPathLocation
- {
- get { return Assembly.GetExecutingAssembly().Location; }
- }
- }
Lastly, to further visualize, see figure 1 for the project structure. The entire project can also be downloaded here.

Figure 1
Explore and Play with System.IO.Path
Path class, it is a static class which provides common operations on dealing with filenames, directories, and paths. It is commonly known on String instances that contain files and/or directory path information. See figure 2 below to visualize the System.IO.Path static class and the methods it provides.

Figure 2
Let us see the examples below.
In the example below, we have tried to explore the methods HasExtension and GetExtension which is straightforward.
- [TestMethod]
- [Priority(1)]
- public void Test_PathLocation_If_Has_FileExtension_And_CheckExtension()
- {
- //true because the path has a file extension
- Assert.IsTrue(Path.HasExtension(projectPathLocation));
- //gets the file extension
- string projectFileExtension =
- Path.GetExtension(projectPathLocation);
- //true because the file extension of the assembly is .dll
- Assert.IsTrue(projectFileExtension == ".dll");
- }
- [TestMethod]
- [Priority(2)]
- public void Test_Path_Method_GetFileName_Wihout_Extension()
- {
- //try to get the file-name only forget about the extension
- string fileName =
- Path.GetFileNameWithoutExtension(projectPathLocation);
- //true
- Assert.AreEqual(fileName, "Playing_With_File_IO_Csharp");
- }
- [TestMethod]
- [Priority(3)]
- public void Test_Path_Method_GetFile_Then_Change_FileExtension()
- {
- //pattern to verify file extension
- string pattern = @"([^\\]+)\.{0}$";
- //file-type extension
- string[] fileExtensions = new string[] { "txt", "docx" };
- string fullPath =
- Path.Combine(projectPathLocation,
- "Playing_With_File_IO_Csharp.txt");
- //true
- Assert.IsTrue(Regex.IsMatch(fullPath,
- string.Format(pattern, fileExtensions[0])));
- string changedPath = Path.ChangeExtension(fullPath, ".docx");
- //true
- Assert.IsTrue(Regex.IsMatch(changedPath,
- string.Format(pattern, fileExtensions[1])));
- }
In the last example below, we have tried to explore the methods GetTempPath, GetTempFileName which is really helpful for developers when dealing with temporary files. If we have an application that deals with temporary files and dump those files to temp-path, these two methods are very handy.
- [TestMethod]
- [Priority(4)]
- public void Test_Path_Method_GetTempFilePath_And_CreateTemporaryFile()
- {
- //gives you the location for the temporary folder
- string temporaryPath = System.IO.Path.GetTempPath();
- //lets just check if the temporary path do exists
- Assert.IsTrue(Directory.Exists(temporaryPath));
- //lets create a new (*.tmp) temporary file on disk
- string tempFileName = System.IO.Path.GetTempFileName();
- //lets check if the temporary file exists
- Assert.IsTrue(File.Exists(tempFileName));
- }
Explore and Play with System.IO.Directory
Directory class is a static class which provides a lot of methods for dealing with directories. It exposes a lot of methods for creating, enumerating directories and subdirectories. However; I can’t discuss everything but will try to explore some. See figure 3 to visualize the System.IO.Directory static class and the methods it provides.

Figure 3
Let us see the examples below.
In the example below, we have shown how to create a new directory using the CreateDirectorymethod. Upon successfully creating a directory the method CreateDirectory returns a DirectoryInfoclass which you can use to get information about the directory, sub-directories, and files.


Join the conversation! Your thoughts help the community grow.