Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi friends,
In my Project, I get a information from a folder to Datagridview. Information are like filename,filetype,filesize and so on..My question is How to change the filename when i press rename(Contextmenu) menu at runtime .Please urgent ..
Regards,
Lakshmi Narayanan.S
Posted
Updated 18-Oct-18 10:22am

You Can use this Code to rename a file , although you Can Alternatively use Code in Second Solution Given at Next Renaming Way:-

FileInfo fi = new FileInfo("NameToBeRename.jpg");
{
if (!File.Exists("NewName.jpg"))
{
fi.MoveTo("NewName.jpg");
}
}

You also use following Code

if (!File.Exists("NewName.jpg" && File.Exists("NameToBeRename.jpg")))
{
File.Move("NameToBeRename.jpg,"<pre>NewName.jpg"</pre>");
}
 
Share this answer
 
Comments
André Kraak 6-Oct-11 4:33am    
This is a repost.
It looks like you intended you change your original solution, you can do so by using the Improve solution button located below the solution.

Please do so and remove this duplicate solution.

Thanks.
P.Vinoth 17-Aug-12 4:24am    
same error friend.

Thank you very much for your posting
Sandeep Gurjar 17-Jan-17 1:57am    
thank u Mr.Gurjar
Reagrd
sandeep gurjar
To rename a file use the following :
C#
File.Move("sourcename.ext","newname.ext");


Renames are implemented as a move in c#.
 
Share this answer
 
Comments
naraayanan 6-Oct-11 2:56am    
hi ,
thanks for your reply.
You Can use this Code to rename a file , although you Can Alternatively use Code in Second Solution Given at Next Renaming Way:-

FileInfo fi = new FileInfo("NameToBeRename.jpg");
{
if (!File.Exists("NewName.jpg"))
{
fi.MoveTo("NewName.jpg");
}
}

You also use following Code

if (!File.Exists("NewName.jpg" && File.Exists("NameToBeRename.jpg")))
{
File.Move("NameToBeRename.jpg,"
NewName.jpg"
");
}
 
Share this answer
 

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