none
Help with a signal that the directory was moved sucessfully RRS feed

  • Question

  • Hi All,

    I'm currently using this a save function.   "My.Computer.FileSystem.MoveDirectory(sourceDirectoryName, destinationDirectoryName)"

    How do I use this or something similar to be able to prompt a message saying that the file was successfully moved?

    Thanks!

    Thursday, April 4, 2019 6:08 PM

Answers

  • Hi,

    Use asynchronous 

    Imports System.IO
    
    Public Class Form1
        Delegate Sub CopyDirectoryEventHandler(ByVal OldDirectoryName As String, ByVal NewDirectoryName As String)
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CopyDirectory("C:\A", "D:\B")
        End Sub
        Private Sub CopyDirectory(ByVal OldDirectoryName As String, ByVal NewDirectoryName As String)
            Dim t = New CopyDirectoryEventHandler(Sub(ByVal sourceFileName As String, ByVal destFileName As String)
                                                      CopyDir(OldDirectoryName, NewDirectoryName)
                                                  End Sub).BeginInvoke(OldDirectoryName, NewDirectoryName, New AsyncCallback(Sub(ByVal Result As IAsyncResult)
                                                                                                                                 MessageBox.Show("moved sucessfully")
                                                                                                                             End Sub), Nothing)
        End Sub
        Public Shared Sub CopyDir(ByVal fromDir As String, ByVal toDir As String)
            If Not Directory.Exists(fromDir) Then Return
    
            If Not Directory.Exists(toDir) Then
                Directory.CreateDirectory(toDir)
            End If
    
            Dim files As String() = Directory.GetFiles(fromDir)
    
            For Each formFileName As String In files
                Dim fileName As String = Path.GetFileName(formFileName)
                Dim toFileName As String = Path.Combine(toDir, fileName)
                File.Copy(formFileName, toFileName)
            Next
    
            Dim fromDirs As String() = Directory.GetDirectories(fromDir)
    
            For Each fromDirName As String In fromDirs
                Dim dirName As String = Path.GetFileName(fromDirName)
                Dim toDirName As String = Path.Combine(toDir, dirName)
                CopyDir(fromDirName, toDirName)
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mpwbs92 Monday, April 8, 2019 3:47 PM
    Friday, April 5, 2019 2:26 AM

All replies

  • Hello,

    Since MoveDirectory is a procedure, not a function you can't learn when the operation has completed.

    If moving a directory with no sub-directories you could create the folder using Directory.CreateDirectory, in the directory to move from use Directory.GetFiles. Now iterate the files and copy them one by one to the destination folder, check it exists, remove the original.

    Does this make sense to you?


    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, April 4, 2019 6:42 PM
    Moderator
  • Hi

    You could use IO.Directory.Exists(destinationDirectoryName) and IO.Directory.Exists(sourceDirectoryName) after the Move operation. If the source no longer exists and the destination does, then it might be fair to assume the Move was successful (not 100%, but a high likelyhood).

    To get 100%, you might have to check each file individually, which of course ruins the idea of moving on block.

    Of course, you could have a Try....Catch to report errors that may stop the Move operation.


    Regards Les, Livingston, Scotland

    Thursday, April 4, 2019 6:50 PM
  • I'm currently using this a save function.   "My.Computer.FileSystem.MoveDirectory(sourceDirectoryName, destinationDirectoryName)"

    How do I use this or something similar to be able to prompt a message saying that the file was successfully moved?

    The standard method is with  IFileOperationProgressSink

    but it needs a lot more code (P/Invoke)...


    Thursday, April 4, 2019 6:53 PM
  • Hi,

    Use asynchronous 

    Imports System.IO
    
    Public Class Form1
        Delegate Sub CopyDirectoryEventHandler(ByVal OldDirectoryName As String, ByVal NewDirectoryName As String)
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CopyDirectory("C:\A", "D:\B")
        End Sub
        Private Sub CopyDirectory(ByVal OldDirectoryName As String, ByVal NewDirectoryName As String)
            Dim t = New CopyDirectoryEventHandler(Sub(ByVal sourceFileName As String, ByVal destFileName As String)
                                                      CopyDir(OldDirectoryName, NewDirectoryName)
                                                  End Sub).BeginInvoke(OldDirectoryName, NewDirectoryName, New AsyncCallback(Sub(ByVal Result As IAsyncResult)
                                                                                                                                 MessageBox.Show("moved sucessfully")
                                                                                                                             End Sub), Nothing)
        End Sub
        Public Shared Sub CopyDir(ByVal fromDir As String, ByVal toDir As String)
            If Not Directory.Exists(fromDir) Then Return
    
            If Not Directory.Exists(toDir) Then
                Directory.CreateDirectory(toDir)
            End If
    
            Dim files As String() = Directory.GetFiles(fromDir)
    
            For Each formFileName As String In files
                Dim fileName As String = Path.GetFileName(formFileName)
                Dim toFileName As String = Path.Combine(toDir, fileName)
                File.Copy(formFileName, toFileName)
            Next
    
            Dim fromDirs As String() = Directory.GetDirectories(fromDir)
    
            For Each fromDirName As String In fromDirs
                Dim dirName As String = Path.GetFileName(fromDirName)
                Dim toDirName As String = Path.Combine(toDir, dirName)
                CopyDir(fromDirName, toDirName)
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mpwbs92 Monday, April 8, 2019 3:47 PM
    Friday, April 5, 2019 2:26 AM