Search code examples
vb.netreferencerelative-path

Relative references when form loads in AppData folder


I've got a form that can be loaded from USB stick, from my home computer, or from work computers. The form executable is always stored in "some path\National Payroll\Build* and references a database in "some path\National Payroll".

The some path part changes, but the database is always in the parent directory. I've tried other advice to get the form's path, but I can only seem to get the path of the Local AppData folder where the form runs from when it is open, not the path of the form's executable. How do I get that?


Solution

  • Application.StartupPath
    

    And, eventually Application.StartupPath.Parent

    EDIT: TEST REPORT

    Make a new project. Add a form with two TextBoxes named StartupPath and ExecutablePath. In the Form's Load event put the following code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StartupPath.Text = Application.StartupPath
        ExecutablePath.Text = Application.ExecutablePath
    End Sub
    

    Compile and deploy the executable on a USB flash drive (or wherever you want) and double click. This output will be produced: enter image description here