0

I am using file.copyfile to rename my file but it doesnt seem to be working the filename is staying the same ?

    Dim _filename As String
    Dim _path As String
    Dim _newfilename As String

    _filename = Path.GetFileNameWithoutExtension(filename)
    _path = Path.GetDirectoryName(filename)
    _newfilename = _filename & "." & extension
    My.Computer.FileSystem.CopyFile(filename,
    _newfilename, True)
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • 3
    Copying a file does not rename a file - it copies it. Please can you edit your question to clarify exactly what you are trying to do? You probably want to look at File.Move (http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp) – RB. Apr 29 '14 at 13:03
  • according to the documentation it will rename the file indeed http://msdn.microsoft.com/en-us/library/36xbexyf(v=vs.90).aspx – Archlight Apr 29 '14 at 13:10
  • The documentation does not say it will rename the original file. It can overwrite an existing file or copy to a new name (rename), but the original file wills tay the same unless you Move it as per the other comments – Allan S. Hansen Apr 29 '14 at 13:13
  • Sorry.. Misread RB's answer. – Archlight Apr 29 '14 at 13:13
  • @SysDragon It might be a conceptual duplicate, but frankly that answer sucks. Is that the best dupe out there? If so, then a more complete answer here would serve the community better. – DonBoitnott Apr 29 '14 at 14:10
  • @DonBoitnott Or a better answer there. A duplicate question is independent of the answers. – SysDragon Apr 29 '14 at 15:00

1 Answers1

0

you could try this:

My.Computer.FileSystem.RenameFile(currentFileName, NewFileName)

http://msdn.microsoft.com/en-us/library/5w05844e.aspx

I think your code below is setting _newfilename to be the name of the file and is not including a directory. The file is being copied to the root folder where the application is running I think. RenameFile should work ok for what you need I think. Let me know if this is ok.

_newfilename = _filename & "." & extension
Johnny Fitz
  • 532
  • 2
  • 8