In daily life, especially for those dealing with file management, organizing file names is a common need. For example, you may want to convert all file names in a folder to lowercase. Doing this manually can be time-consuming and prone to errors. In this article, we will develop a simple application using the C# programming language and Windows Forms to automatically convert all file names in a folder to lowercase.

Purpose of the Application
This application aims to simplify file management by converting all file names in a user-selected folder to lowercase. The application also shows the user which file names have been changed.
How the Application Works
- Folder Selection: The user selects a folder.
- File Listing: All files in the selected folder are listed.
- File Name Conversion: Each file name is converted to lowercase, and the file name is updated.
- Display Results: The changed file names are displayed to the user.
Code Analysis
Below, we will examine the basic code structure of the application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.IO; using System.Windows.Forms; namespace FileNameConverter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSelectFolder_Click(object sender, EventArgs e) { lstFiles.Items.Clear(); // Clear the ListBox using (FolderBrowserDialog fbd = new FolderBrowserDialog()) { if (fbd.ShowDialog() == DialogResult.OK) { string folderPath = fbd.SelectedPath; try { string[] files = Directory.GetFiles(folderPath); foreach (string file in files) { string fileName = Path.GetFileNameWithoutExtension(file); string extension = Path.GetExtension(file); // Convert file name to lowercase string newFileName = fileName.ToLower() + extension; // Create the new file path string newFilePath = Path.Combine(folderPath, newFileName); // Rename the file File.Move(file, newFilePath); // Add the change to the ListBox lstFiles.Items.Add($"{Path.GetFileName(file)} >>> {newFileName}"); } MessageBox.Show("All file names have been converted to lowercase."); } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } } } } |
Explanation of the Code
- Folder Selection: The
FolderBrowserDialog
allows the user to select a folder. After the user selects a folder, the path of the selected folder is retrieved using theSelectedPath
property. - File Listing: The
Directory.GetFiles
method retrieves the paths of all files in the selected folder and stores them in an array. - File Name Conversion: For each file, the
Path.GetFileNameWithoutExtension
andPath.GetExtension
methods are used to separate the file name and extension. The file name is converted to lowercase, and a new file path is created. TheFile.Move
method is used to rename the file. - Display Results: The changed file names are added to the
ListBox
control and displayed to the user.
Error Handling
The application uses a try-catch
block to catch any errors that may occur while renaming files. For example, if a file with the same name already exists or if the file is in use, an error message is displayed to the user.
Conclusion
This application provides a simple and effective solution for users who want to automatically convert file names to lowercase. Developed using C# and Windows Forms, this application serves as a basic example for simplifying file management tasks. Such applications can be time-saving, especially for users working with large numbers of files.
In this article, we developed a simple file renaming application using C#. This example demonstrates how powerful the C# language is for file operations and user interface creation. Applications like this can serve as a starting point for developing more complex file management tools.