file rename in VB .net

Hello

I try to rename a file by

file.copy(oldPathName, newPathName)

then

file.delete(oldPathName)

File.copy works. But somehow, a message saying oldPathName is being used by another process, when it tries to delete the old file.

Any idea why this happened and how to make it works.

Thanks

Quin



Answer this question

file rename in VB .net

  • Kevin Hoffman

    Are you opening the 'oldPathName' anywhere in your code If so it's likely that a handle isn't being disposed properly and it's holding onto the old file.

  • mkb2006

    Why not use the My.Computer.FileSystem.RenameFile Method to do the job rather than creating your own combination of copy and delete. There are more issues involved in recreating your own two step process such as timing, and fallback if failure occurs. I've found its almost always better to use whats available rather than trying to recreate the wheel.

    My.Computer.Filesystem.RenameFile
    http://msdn2.microsoft.com/en-us/library/2ce15by3.aspx


  • nglow

    in addition, what happens if you place a small delay for about say...300 milliseconds between the operations via the System.Threading.Thread.Sleep() method

  • file rename in VB .net