none
How to reference path to image file in bin\Debug folder RRS feed

  • Question

  • Hi,

    I have a WinForm app whose executable needs to be copied to multiple clusters.

    My problem is when I copy the executable, the path to an image file doesn't seem to be embedded with the executable.  Here are my code snippets:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	''Set chart area background image
            Dim binPath As String = AppDomain.CurrentDomain.BaseDirectory
            Dim imgPath As String = binPath + "images\blue2.jpg"
            cpu_chart.ChartAreas(0).BackImage = imgPath
    End Sub

    I'm getting an error:  "Could not find a part of the path" because the binPath is being changed to another Drive letter.  Is there a way to make it point to a path that doesn't change when I copy it to another computer?

    Can anyone help?  Appreciate it.


    Marilyn Gambone




    • Edited by deskcheck1 Friday, November 8, 2019 9:12 PM
    Friday, November 8, 2019 8:56 PM

Answers

  • multiple errors here

    Let's say the exe 's path is c:\test\a.exe and you want to read c:\test\images\blue2.jpg

    your code will yield c:\testimages{backspace}lue2.jpg

    fix:

    use Path.Combine to combine paths when you are not sure if a folder ends with a backslash.

    prefix a string literal with @ if you do not intend to use escape sequence in the string. or use escape sequence in the string correctly. 



    Visual C++ MVP

    • Marked as answer by deskcheck1 Saturday, November 9, 2019 12:48 AM
    Friday, November 8, 2019 10:50 PM
    Moderator

All replies

  • multiple errors here

    Let's say the exe 's path is c:\test\a.exe and you want to read c:\test\images\blue2.jpg

    your code will yield c:\testimages{backspace}lue2.jpg

    fix:

    use Path.Combine to combine paths when you are not sure if a folder ends with a backslash.

    prefix a string literal with @ if you do not intend to use escape sequence in the string. or use escape sequence in the string correctly. 



    Visual C++ MVP

    • Marked as answer by deskcheck1 Saturday, November 9, 2019 12:48 AM
    Friday, November 8, 2019 10:50 PM
    Moderator
  • Hi,

    I've done that.  It still doesn't work.  I found a "workaround" to it via brute force :)

    I hard-coded the directory of my image in my dev machine's C: drive and created the same directory in my target machine.  So now, even if my path string is:

    C:\mydir\images\blue2.jpg

    it will be the same in the target machine I'm deploying it to.

    Unless anyone has a better idea, I'll keep this.

    Thanks for the reply.


    Marilyn Gambone

    Saturday, November 9, 2019 12:48 AM
  • Forgot u have a VB app. Application.StartupPath will give you the path for the running executable file. You can then use Path.GetDirectoryName to get the parent directory and use Path.Combine to get to the file in your image folder. 


    Visual C++ MVP

    Saturday, November 9, 2019 1:11 AM
    Moderator
  • Hi,

    Tried it.  Still doesn't work.  It still returns just the imgPath, even if I use Path.Combine.  I don't know why Path.Combine doesn't work in VB.  I always end up concatenating the multiple paths.

    Thanks for the response, though.


    Marilyn Gambone

    Saturday, November 9, 2019 12:21 PM