none
Help moving a file.... RRS feed

  • Question

  • HI, I'm in need of some advice in moving a file I found some sources out there and I tried them out but every time I try, it throws an error like it can't do its operation. Below is what I've tried generically I can't divulge in where I'm actually trying to place the file. I know doing a directory works with the string format, wondering if it would with the file move format. Just putting it out there for suggestions. Thanks!

     Private Sub CmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click

            Dim x As String = Convert.ToString(txtBarcodeInput.Text)
            Dim objDir As New DirectoryInfo("C:\Users\Owner\Documents\TestFolder\ " + x + DateString + TimeString)
            'Dim objDir1 As New DirectoryInfo("C:\Users\Owner\Desktop\Test")
            Dim FileToMove As String
            Dim MoveLocation As String

    Try

                objDir.Create()
            Catch
                Throw New Exception("Failed to create a new directory")
            End Try

            FileToMove = "C:\Users\Owner\Desktop\Test"
            MoveLocation = "C:\Users\Owner\Desktop\TestFolder\ " + x + DateString + TimeString

           If File.Exists(FileToMove) = True Then
                File.Move(FileToMove, MoveLocation)
                MessageBox.Show("Files Moved")
            End If

    End Sub

    Thursday, March 28, 2019 9:04 PM

All replies

  • Hi

    Firstly, please use the code block tool from the toolbar when posting code. (like below)

    *

    Here is an example that works fine. It is using hard coded paths, but they could just as easily be derived paths.

    *

    One thing. You say you can't divulge where it is being moved to, but be warned, some destinations are 'out of bounds' depending on OS and other security factors.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim sourcepath As String = "C:\Users\lesha\Desktop\New folder\GIGABLUE BOXES - Copy (2).txt"
      Dim destpath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\GIGABLUE BOXES - Copy (2).txt"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        IO.File.Move(destpath, sourcepath)
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, March 28, 2019 9:17 PM
    Thursday, March 28, 2019 9:14 PM
  • Hello,

    Here are some high level tips

    • Never use string concatenation e.g. & or + for creating a path, always use Path.Combine
    • Turn Option Strict On
    • Never use a variable of a single character unless in a small for/next.
    • Set a breakpoint, step through the code that is not running right and as you step through examine variable values against what you believe they should be.
    • Ensure the program has proper file permissions.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 28, 2019 9:40 PM
    Moderator
  • Without knowing the exact error it makes it difficult but it is possible it is a permissions problem.  Even if you are logged in as administrator to the computer you can't access another user's desktop. You can validate permissions before you try to move the file with code like below.  Try to tell us what the exact error message is.

            Dim strErrorMessage As String = ""
            If Not (DoIHavePermissionToWrite("C:\Users\,<username>\Desktop", strErrorMessage)) Then
                MsgBox(strErrorMessage)
            End If
        Private Function DoIHavePermissionToWrite(pstrFolderPath As String, ByRef pstrErrorMessage As String) As Boolean
            DoIHavePermissionToWrite = False
            Try
                Dim fileNames = My.Computer.FileSystem.GetFiles(pstrFolderPath, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
                Dim strTestFileName As String = pstrFolderPath & "\test123.txt"
                Dim fh As Integer = FreeFile()
                FileOpen(fh, strTestFileName, OpenMode.Binary, OpenAccess.Write)
                FilePut(fh, "test")
                FileClose(fh)
                If File.Exists(strTestFileName) Then
                    File.Delete(strTestFileName)
                End If
                Return True
            Catch ex As Exception
                pstrErrorMessage = ex.Message
                Return False
            End Try
        End Function
    

    Sunday, March 31, 2019 4:11 AM