Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 fileName = string.Concat
                                          (
                                             "",
                                             Path.GetFileNameWithoutExtension(fileName),
                                             info.Ref_Compteur,
                                             Path.GetExtension(fileName)
       
This solution modifies the value of the field in the database representing the file name not the file as such which is in a directory                                   );


What I have tried:

I want to change the name of the file in the directory.
Posted
Updated 13-Feb-20 0:56am

There is no "rename" function in the C# File classes; what you need to do is to use the File.Move() method
C#
string fileNameOld = fileName;
string fileNameNew = /* insert your new name code here */

string pathFileOld = /* path to old file */
string pathFileNew = /* path to new file */

try {
	if ((File.Exists(pathFileOld) && (!FileExists(pathFileNew)) {
		File.Move(pathFileOld, pathFileNew);
	} else {
		// message: either old file does not exist
		// message: or new file path already exists
	}
} catch(Exception ex) {
	// message: Error + ex.Message
}
Reference: File.Move Method (System.IO) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Maciej Los 7-Feb-20 17:54pm    
Even if it it was already stated by Dave, but deserves for 5!
MadMyche 7-Feb-20 20:21pm    
Thank... it takes me too long to type here
Oddly enough, you move it. The File.Move method gives you the option of supplying a new filename, so you simply move the file to the same folder it's already in and give it a new filename.

File.Move Method (System.IO) | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 7-Feb-20 17:52pm    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900