none
Running code has a different result than debugging code RRS feed

  • Question

  • I have the following code:

            If Directory.Exists("Teams") Then
                Directory.Delete("Teams", True)
            End If

            If Not Directory.Exists("Teams") Then
                InitialFileSetup()
            End If

    Now if I run the code I expect the routine InitialFileSetup to run as I am deleting the folder (directory).  When I run F10 thru the code it works as expected.  If I run the code F5 it does not run the routine.  It seems that the delete when running the code (F5) the code continues before the actual delete is actually complete.

    BTW this code is only there during the developing of the app as I was going to the folder and manually deleting the folder and thought this code would stop me from having to do this each execution.


    Lloyd Sheen

    Friday, October 23, 2020 7:44 PM

All replies

  • I have the following code:

            If Directory.Exists("Teams") Then
                Directory.Delete("Teams", True)
            End If

            If Not Directory.Exists("Teams") Then
                InitialFileSetup()
            End If

    Now if I run the code I expect the routine InitialFileSetup to run as I am deleting the folder (directory).  When I run F10 thru the code it works as expected.  If I run the code F5 it does not run the routine.  It seems that the delete when running the code (F5) the code continues before the actual delete is actually complete.

    BTW this code is only there during the developing of the app as I was going to the folder and manually deleting the folder and thought this code would stop me from having to do this each execution.


    Lloyd Sheen

    Hi

    Well, I can't test your code.


    Regards Les, Livingston, Scotland

    Friday, October 23, 2020 8:16 PM
  • Have you considered using a build event which only executes under your development environment?

    RD /S /Q "$(TargetDir)\Teams\"
    mkdir $(TargetDir)\Teams

    A step farther, we want to copy a file from the project folder to the Team folder.

    RD /S /Q "$(TargetDir)\Teams\"
    mkdir $(TargetDir)\Teams
    xcopy "$(ProjectDir)applications.json" "$(TargetDir)Teams" /Y


    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

    Friday, October 23, 2020 9:56 PM
    Moderator
  • I have the following code:

            If Directory.Exists("Teams") Then
                Directory.Delete("Teams", True)
            End If

            If Not Directory.Exists("Teams") Then
                InitialFileSetup()
            End If

    Now if I run the code I expect the routine InitialFileSetup to run as I am deleting the folder (directory).  When I run F10 thru the code it works as expected.  If I run the code F5 it does not run the routine.  It seems that the delete when running the code (F5) the code continues before the actual delete is actually complete.


    First off, if I use this code:

    Sub Main()
        If Directory.Exists("Teams") Then
            Directory.Delete("Teams", True)
        End If
    
        If Not Directory.Exists("Teams") Then
            'InitialFileSetup()
            Directory.CreateDirectory("Teams")
        End If
    End Sub
    

    I am not seeing any lag. Specifically I do the following:

    (1) Run the code (F5).

    (2) Look at the directory where the "Teams" subdir gets created using
    Windows Explorer, (...\bin\Debug) and note the time stamp of the "Teams"
    folder.

    (3) When the clock ticks to the next minute, run the code again (F5).
    This should delete the file with the original time stamp and recreate it with
    the current time.

    (4) Verify the time stamp on the file in Explorer.

    After repeated testing, the "Teams" directory is always recreated and has the
    correct (current) time stamp. I never encountered a case where the directory
    wasn't recreated.

    Note however that these tests were all done with an empty "Teams" folder.
    Presumably the delete of the directory will take longer with multiple
    files in it.

    To test your theory that it is a timing/latency issue, have you tried
    adding a timed delay between the two if() statements?

    If Directory.Exists("Teams") Then
        Directory.Delete("Teams", True)
    End If
    
    Thread.Sleep(2000) ' wait 2 seconds (adjust to taste)
    
    If Not Directory.Exists("Teams") Then
        InitialFileSetup()
    End If
    

    - Wayne

    Saturday, October 24, 2020 9:49 AM
  • Hi sqlguy,

    You can consider using 'FileSystemWatcher' class to listen to the file system and notify if any changes are made to the directory.

    Here's the code you can refer to.

       Sub Main()
            Dim watcher As FileSystemWatcher = New FileSystemWatcher()
            watcher.Path = "your path"
            watcher.IncludeSubdirectories = True
            watcher.NotifyFilter = NotifyFilters.Attributes Or NotifyFilters.CreationTime Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.Security Or NotifyFilters.Size
            watcher.Filter = "*.*"
            AddHandler watcher.Deleted, AddressOf OnChanged
            watcher.EnableRaisingEvents = True
            Console.ReadLine()
        End Sub
        Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
            Console.WriteLine($"{e.Name}, with path {e.FullPath} has been {e.ChangeType}")
            InitialFileSetup()
        End Sub

    Hope it could be helpful.

    Besides, if your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    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.


    Monday, November 2, 2020 8:39 AM
    Moderator