Javatpoint Logo
Javatpoint Logo

C# Rename File


C# (C Sharp) was developed by Microsoft, and it is a programming language which is based on an object-oriented paradigm. C# has similar syntax like other programming languages like Java, C++ or Python, and it is very easy to learn.

C# is used for a wide range of applications, including developing Windows desktop applications, web applications, mobile apps, video games, and more. It is a high-level language which means it can be understood very easily by humans because it is not tough to understand like machine language.

One of the biggest advantages of working with C# is that it works very well with the .NET framework, which has a lot of functionalities for developing the application. The .NET framework includes libraries for working with databases, networking, user interfaces, security, and more, which makes it easy for developers to create robust, scalable applications.

C# is also known for its strong type checking, which means that the language ensures that data types are used correctly at compile time, reducing the likelihood of runtime errors. C# also supports garbage collection, which means that developers don't have to worry about managing memory manually, as the language automatically frees up memory that is no longer needed.

File renaming is a common task in programming, and it is often necessary when working with files. In C# renaming a file is a relatively simple task that can be accomplished with just a few lines of code. We will examine how to rename a file in C# in this tutorial.

What is File Renaming?

Renaming a file simply means changing its name. For example, if you have a file and it has the name as "example.txt", and you want to rename it to "newexample.txt", you would be renaming the file.

File renaming can be useful for a variety of reasons. For example, you wish to change the name of a file to make it more descriptive or to make it easier to find. Alternatively, you may want to rename a file to avoid conflicts with other files that have similar names.

Renaming a File in C#

Now that we understand what file renaming is, let's understand how to do it in C#. There are several ways to rename a file in C#, but we will focus on the simplest method, which involves using the File.Move() method.

The File.Move() method is a static method of the System.IO.File class that allows you to move or rename a file. When you use the File.Move() method to rename a file, you simply provide the current name of the file and the new name that you want to give it.

Below is an example of how to rename a file using the File.Move() method:

C# code:

In this example, we are using the File.Move() method to rename a file named "example.txt" to "newexample.txt". The first parameter of the method is the current name of the file, and the second parameter is the new name that we want to give it.

It is important to analyze that when you use the File.Move() method to rename a file, you must provide the full path to both the current file and the new file. In this example, we are using absolute file paths, but you could also use relative file paths if you prefer.

Error Handling:

When you are working with files in C#, it is important to handle errors that may occur. When you try to rename a file, there are several errors that could occur, such as the file not existing or the file already existing with the new name.

To handle these errors, you can use try-catch blocks to catch any exceptions that are thrown by the File.Move() method. Below is an example of how to use try-catch blocks to handle errors when renaming a file:

C# code:

In this example, we are using try-catch blocks to catch two possible exceptions that could be thrown by the File.Move() method: FileNotFoundException and IOException. If the file does not exist, the catch block for FileNotFoundException will be executed. If an I/O error occurs while trying to rename the file, the catch block for IOException will be executed.


Renaming a file in C# is a relatively simple task that can be accomplished with just a few lines of code. The File.Move() method is the simplest way to rename a file in C#.

Next TopicC# Validate Email

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA