Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I wanna know how exactly rename in c# works
For example , I wanna select folder and it should rename *.dat to *.bmp
Also , for the selected file .

Though in


C#
File.Copy(textBox1.text, textBox2.text);

File.Delete(textBox1.text);

Where textBox1.text has the method for browse a file , and it will write the path there
Where textBox2.Text has the place to type your new type example: *.png *.mp3 etc.

but It wont work 100% :eek:

So , can I get help?:o
Posted

Use File.Move(sourceFilePath, destFilePath)
If the only difference in the file paths is the name, then the file will just be renamed, not relocated.
Be sure the destFilePath doesn't already exist, as File.Move() will not replace an existing file.
(And, of course, standard issues of permissions apply.)
 
Share this answer
 
Use move instead.
http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx[^]

That way the file is moved instead of going through the overhead of recreating and deleting.
 
Share this answer
 
Rename a file is nothing but the overwriting to the same file name. You can achieve that using the same File.Copy method with bool flag true as third parameter.
File.Copy(textBox1.text, textBox2.text,true);
 
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