VB.Net, very simple app - just a double check

Soldato
Joined
5 Aug 2006
Posts
4,261
As far as i can from running this application on my mums laptop its working fine, but i just want people to double check, basicly im trying to backup her work and stuff from her laptop to my desktop (hence im using fixed paths)

This will run at login but her laptop can take like 10seconds-1minute to connect to wireless network so i've added in a little wait timer style thing, so it will look for shared location, if its not found hopefully sleep for 10seconds and try again :)

as i said it seems to be working but if people could just double check (source code wise anyway!), its not too clever - just overwrites stuff, dosent check for newer files etc. but thats not too important to be frank.

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Main:
        If My.Computer.FileSystem.DirectoryExists("\\Alec-Desktop-1\Profile\") Then
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Documents\", "\\Alec-Desktop-1\Profile\Documents\", True)
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Desktop\", "\\Alec-Desktop-1\Profile\Desktops\", True)
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Favorites\", "\\Alec-Desktop-1\Profile\Favorites\", True)
            Close()
        Else
            System.Threading.Thread.CurrentThread.Sleep(10000)
            GoTo Main
        End If

    End Sub
End Class

Also despite having a form attached it dosent seem to display, just runs in the background - this isn't a problem but just curious as to why this might be :)

ta

alec
 
Answerd by own bit about window appearing, just looked over an old thread of mine. Multithreading !

Code:
Imports System.Threading
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim t As Thread
        t = New Thread(AddressOf Me.BGCopy)
        t.Start()

    End Sub
    Private Sub BGCopy()

Main:
        If My.Computer.FileSystem.DirectoryExists("\\Alec-Desktop-1\Profile\") Then
            lblStatus.Text = ("Backing Up...")
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Documents\", "\\Alec-Desktop-1\Profile\Documents\", True)
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Desktop\", "\\Alec-Desktop-1\Profile\Desktops\", True)
            My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Favorites\", "\\Alec-Desktop-1\Profile\Favorites\", True)
            Close()
        Else
            lblStatus.Text = ("Looking for network connection...")
            System.Threading.Thread.CurrentThread.Sleep(10000)
            GoTo Main
        End If
    End Sub

End Class


so another questiom

how do i start making logfiles? or idealy writting things to event log ? becuase obviously atm i have no idea if things are working (unless i go look at files spesificaly!)
 
Last edited:
Not too clever with my VB, so wont attempt any code...

However, built-in functions like CopyDirectory should return an exit code depending on if it succeeded.

You could capture that return code and depending on its value add a particular entry to a log using VB's file writing functions.

You should be able to find out what CopyDirectory returns by looking at the documentation.
 
Right got this now. Its not clever but its enough that i can atleast work out any errors that may be occuring :) thanks for your help. When im a bit better with VB i might expand on this little program but seems to be working good enough for now! ta

Code:
Imports System.Threading
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim t As Thread
        t = New Thread(AddressOf Me.BGCopy)
        t.Start()

    End Sub
    Private Sub BGCopy()

Main:
        If My.Computer.FileSystem.DirectoryExists("\\Alec-Desktop-1\Profile\") Then
            lblStatus.Text = ("Backing Up...")
            Try
                My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Documents\", "\\Alec-Desktop-1\Profile\Documents\", True)
                My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Desktop\", "\\Alec-Desktop-1\Profile\Desktops\", True)
                My.Computer.FileSystem.CopyDirectory("C:\Users\Frances\Favorites\", "\\Alec-Desktop-1\Profile\Favorites\", True)
            Catch err As Exception
                lblStatus.Text = err.ToString
            End Try
            If lblStatus.Text = "Backing Up..." Then
                Close()
            Else
                Dim oFile As System.IO.File
                Dim oWrite As System.IO.StreamWriter
                oWrite = oFile.CreateText("errorlog.txt")
                oWrite.Write(lblStatus.Text.ToString)
                oWrite.Close()
                Close()
            End If
        Else
            lblStatus.Text = ("Looking for network connection...")
            System.Threading.Thread.CurrentThread.Sleep(10000)
            GoTo Main
        End If
    End Sub

End Class

any other observations or anything let me now!

ta

alec
 
Try-Finally should also have a Finally block too. That would be the more logical place to put a close statement - rather than having another IF.
 
Ah cool - didn't even know it had a finally bit :) ta

that program is working for the time being, started on another slightly more intellegent one which is going well - hopefully have it done in a few weeks :)
 
Back
Top Bottom