Click here to Skip to main content
14,495,113 members
Rate this:
Please Sign up or sign in to vote.
See more:
 fileName = string.Concat
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.
Updated 13-Feb-20 0:56am
Rate this:
Please Sign up or sign in to vote.

Solution 2

There is no "rename" function in the C# File classes; what you need to do is to use the File.Move() method
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[^]
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
Maciej Los 7-Feb-20 17:52pm

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100