TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# File.Move Method, Rename File

This C# example uses the File.Move method from the System.IO namespace.

File.Move renames a file. It is a tested .NET Framework method.

The File class in the System.IO namespace provides this convenient method. It performs a fast rename of the file you target. It sometimes throws exceptions.

Example. The method that renames files is called File.Move. You must include the System.IO namespace at the top with a using directive or specify the fully qualified name "System.IO.File.Move".

And: This method receives two string type parameters, which can be dynamic from your program or hard-coded as string literals.

String Literal

Also, we see the verbatim string literal syntax that uses the @ symbol. The program has different results depending on whether the source file exists, and whether the target file already exists.

C# program that renames files with File.Move

using System;
using System.IO;

class Program
{
    static void Main()
    {
	//
	// Move a file found on the C:\ volume.
	// If the file is not found (SAM.txt doesn't exist),
	// then you will get an exception.
	//
	try
	{
	    File.Move(@"C:\SAM.txt", @"C:\SAMUEL.txt"); // Try to move
	    Console.WriteLine("Moved"); // Success
	}
	catch (IOException ex)
	{
	    Console.WriteLine(ex); // Write error
	}
    }
}

Possible exception from program

System.IO.FileNotFoundException: Could not find file 'C:\SAM.txt'.
File name: 'C:\SAM.txt'
   at System.IO.__Error.WinIOError...

The program first includes the System namespace and the System.IO namespace so that the File class, the Console class, and the IOException classes can be directly used. The comment in the Main entry point explains some of the behavior.

The File.Move method is invoked by passing two references to strings that contain path characters. Internally, File.Move uses system routines to attempt to change the name of the first file to the name of the second file.

So: If successful, the first file will no longer exist. If unsuccessful, the operation will be terminated—nothing will be changed on disk.

Exceptions. Whenever you are dealing with the file system, errors will occur. You must be prepared to recover from these errors (even if recovery means exiting). It is usually a mistake not to use exception handling around methods like File.Move.

In the above example, if the "SAMUEL.txt" file on the C volume already exists, you will get another exception. To solve this problem, you can check the target path with the File.Exists method before attempting the File.Move.

File.ExistsIOException

System.IO.IOException:

Cannot create a file when that file already exists.

Uses. We mention some uses of File.Move. We determine when it is preferable to File.Copy and other approaches. Internally, File.Move uses the Win32Native.MoveFile function, which does a rename on the file system.

If you use File.Copy, you will actually be copying the data on the disk, which will be more resource-intensive and slower. However, you should avoid File.Move if you need to retain two copies of the data.

Summary. We renamed files in using the .NET Framework's File.Move method. We looked at the usage of this method on a file that exists and does not exist, and then noted some exceptions caused by this method.

Finally: We peeked inside the .NET Framework's implementation of File.Move and mentioned the difference between File.Copy and File.Move.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf