Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# File & Directory Handling

1. File Operation in C#

C# provides many utility classes to work with the file systems. The DriveInfo class will give us the information about a specific drive in the windows file system. Other similar classe DirectoryInfo provides information about the specific directory and all the file it contains. Similarly FileInfo class provides information about the file and File class provides API functions to perform File Copy and Move actions. In this example, we will explore File system access by using the dot net provided API functions.

2. About the Example

The example we are going to create is below:

Fig 1. About the Example
Fig 1. About the Example

Here, when you click the Show All Drives button, the example will list all the drive letters in the text box below the clicked button. The Directories and Files buttons list the directories and files of the specific directory in the multiline text box located below them. You can enter this specific directory in the Base Directory text box.

Create button of this sample application adds a new directory to the path specified in the Base Directory text box. Before clicking the Create button, we should type the new directory name in the New Directory Name text box. The Copy and Move buttons are used to test the file copy and move functionalities.

3. Display All Drive Letters

In the below code, GetDrives method of the DriveInfo class fetches all the logical drives in the windows file system in the form of DriveInfo array and we store that in a variable called drives. Foreach loop iterates through this array and lists some of the important properties of the drive by reading the DriveInfo properties.

Clicking the Show All Drives button produces the following result:

Fig 2. Showing Drives
Fig 2. Showing Drives

4. List Sub-Directories of a Directory

C # provides two classes to retrieve files and directories information. The DirectoryInfo class gives you files and directories inside them. Each directory is an instance of DirectoryInfo and from this object, we can make a call to the function GetDirectories. So, once we have the base directory we can iterate through every directory in a recursive manner.

In the below code, we construct the DirectoryInfo object by taking the text in the base directory textbox and then we iterate through the DirectoryInfo returned by the GetDirectories function call. Inside the iteration loop, we retrieve the full name of the directory and display it in the multiline textbox.

Now run the sample application and type C:\Windows\System32 in the base directory. To list all the sub-directories, click on the button Directories and the below screenshot shows how the list of directories are displayed in the multi-line text box control.

Fig 3. Showing File and Directories
Fig 3. Showing File and Directories

5. List All Files in a Directory

Once we have DirectoryInfo object, we can also make a call to the GetFiles method to get an array of FileInfo object to represent all the files inside the directory. In the below code, we iterate through the FileInfo array returned by the GetFiles() function call and display the name of the file by accessing the FullName property. For the same C:\Windows\System32 base directory, click on the Files button this time to list all the file names inside the System32 folder.

6. Create Folder via C#

The Create method of the DirectoryInfo will add a new folder to the windows file system. But before making a call to create method, make sure the directory does not exist in the parent path. Have a look at the below code:

In the code, we append the new directory name with the base directory and call the new path to the directory as NewDir. The intension here is to create a new folder within the folder specified in the Base Directory text box. Using this NewDir variable, we create the DirectoryInfo instance and make a call to the Create method. Note, how the code checks the folder already exists or not via the boolean Exists property.

In the below screenshot, we specify the Base Directory as C:\Temp2 and Type the new directory name in the New Directory Name text box. In our case, the new directory name is TestDir. When we click the Create button, a message box appears stating that the new folder is created in the base directory.

Fig 4. Creating Test Directory
Fig 4. Creating Test Directory

7. Copy and Move Files

C# provides File class to handle file related operations. The File class exposes Copy and Move functions to perform file copy and move respectively. Both the functions need two locations in which one represents the source file and other one represents the destination. Now have a look at the below handler codes which make use of the Copy and Move function calls from the File object.

Our sample application has two textboxes to denote source and destination locations and below screenshot shows how these locations are specified in the respective text boxes before clicking the Copy or Move action buttons.

Fig 5. File Copy and Move Operations
Fig 5. File Copy and Move Operations

Categories: C#

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.