What is the way to rename a file on the web server in code

Hi

A few days back I got a question regarding what is the best way to rename a file in the server (say an uploaded File) using Asp.Net 2.0. According to me the best way to rename a file is by using the System.IO namespace and moving the file to the same location with a different Name.

In C#

Using System.IO
File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt")

In VB

Imports System.IO
File.Move("C:\Dir1\SomeFile.txt", "C:\Dir1\RenamedFileName.txt")

[Note: You might get permission error if you do not have the permission to make changes in the folder ]

Hope this helps
Thanks
Vikram


Share this post   Email it

Feedback

Posted on 2/26/2007 3:13:50 AM

hi,

The code is very usefull..resolved my problem..

just wanna know how can i search other topics in the same site..

and can i register myself in this site..

Thanks & regards,
Yunus

Posted on 2/26/2007 4:24:13 AM

Hi Yunus,

Thanks for your appriciation. You can use the search box to search the site. There is no registeration process in the site but if you want you can use the rss and atom feed to get updates when new posts are addded

Posted on 6/26/2008 10:45:27 PM

Hi, this is vikrant, Is there some possible ways to directly rename the file rather move it, because i got, 10 files of 5mb each of them, and it will take much time,

Posted on 11/30/2008 11:59:14 PM

hi
I tried the same piece of code to move a file form one location to the other ( both on the same machine) I got Unauthorized acess Exception, Please help out
tell me what changes i need to do

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia