TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# File.Move Method, Rename File

Rename a file with 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.File
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".String Literal

Here: The program has different results depending on whether the source file exists, and whether the target file already exists.

Info: The program includes the System namespace and the System.IO namespace so that File, Console and IOException can be used.

Next: 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.

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 } } } Output System.IO.FileNotFoundException: Could not find file 'C:\SAM.txt'. File name: 'C:\SAM.txt' at System.IO.__Error.WinIOError...
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.

Info: If the "SAMUEL.txt" file on the C volume already exists, you will get another exception.

Tip: 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.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


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