File.move

Hi there
I am trying to use the move command to move a folder, i have used the code from MSDN and it work pretty well just one problem the file is moved but there is no data inside the moved file. I am sure it is something small but is missing it, here i my code
Try
                If File.Exists(path) = False Then
                    ' This statement ensures that the file is created,
                    ' but the handle is not kept.
                    Dim fs As FileStream = File.Create(path1)
                    fs.Close()
                End If

                ' Ensure that the target does not exist.
                If File.Exists(path2) Then
                    File.Delete(path2)
                End If

                ' Move the file.
                File.Move(path1, path2)

            Catch

            End Try

Open in new window

Andre_pAsked:
Who is Participating?
 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Hello, if your project has Option Explicit = Off it will compile, but if you mistyped File.Exists(path) it always is going to return False (because path is Nothing), so your source file (path1) is always overwrited by Dim fs As FileStream = File.Create(path1), so I think that you need is change File.Exists(path) to File.Exists(path1), result file in path2 is empty.
0
 
CodeCruiserConnect With a Mentor Commented:
Why do you have this part?

If File.Exists(path) = False Then
                    ' This statement ensures that the file is created,
                    ' but the handle is not kept.
                    Dim fs As FileStream = File.Create(path1)
                    fs.Close()
                End If


Also, you have "path" in one place and "path1" in another place. Which one is holding the source file name?
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Ignore last sentence:
, result file in path2 is empty.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.