Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename a file in ASP.NET?

Tags:

asp.net

In my project I want to rename the file before it is updating. For example a file in my system like Mycontact.xls. I want to rename it as sasi.xls (it is an excel file). How can I write the code in ASP.NET?

Actually I am using a fileupload control to get the file in and rename the file and upload the renamed file in a folder which is in Solution Explorer.

like image 328
Surya sasidhar Avatar asked Jan 29 '10 08:01

Surya sasidhar


2 Answers

You can do it with the File.Move method eg:

string oldFileName = "MyOldFile.txt";
string newFileName = "MyNewFile.txt";
File.Move(oldFileName, newFileName);
like image 50
Winston Smith Avatar answered Sep 28 '22 14:09

Winston Smith


C# does not provide a file rename function, unfortunately. Anyhow, the idea is to do this:

File.Copy(oldFileName, NewFileName);

File.Delete(oldFileName);

You can also use - File.Move.

like image 30
Bhaskar Avatar answered Sep 28 '22 13:09

Bhaskar