Paul Mendoza C# blog
Thursday, January 18, 2007
  Rename a file in C# properly

A while back I wrote a C# class that did fairly quick and easy encryption using the Rijndael method. It has two static functions that take a few parameters for the file to encrypt and at the end, I rename the newly created encrypted file to the name of the original file. The problem with this though is that the way I do it renames the file by first calling File.Replace() and then calling File.Delete() which deletes the temporary file that’s being created. On a large file, this can take a really long time to do. If this needs to happen to 30 files, it’s going to be very noticeable that there is a delay every time this needs to happen.

The solution is the use FileInfo.MoveTo() function which is much faster than File.Replace(). Make sure that the file name doesn’t already exist in the directory though.

 
Comments: Post a Comment





<< Home
I am currently an ASP.NET, C# developer working on MangosteenNation.com, a XanGo website for helping people build their businesses. I am also pursuing a degree at CSU San Marcos in Southern California.

XanGo at Mangosteen Nation

Archives
October 2005 / November 2005 / December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / April 2007 / May 2007 / June 2007 / August 2007 / February 2008 / August 2008 /


Powered by Blogger

Subscribe to
Posts [Atom]