locked
Find the path to a file in the solution window RRS feed

  • Question

  • So I added a file to the project. Now I want to open the file to read it. How do I get the path to that file when the program is running? Is the file installed to the project folder where the .exe file is kept? Is this true when you compile the program using VB2012Express and then install it on another machine?

    I tried 

    Dim statefile As String = My.Computer.FileSystem.CombinePath(Application.ExecutablePath, "states.txt") 

    but it returns

    C:\Users\Allen.DWA\Documents\Visual Studio 2012\Projects\JID Treeview List\JID Treeview List\bin\Debug\JID Treeview List.EXE\states.txt

    not

    C:\Users\Allen.DWA\Documents\Visual Studio 2012\Projects\JID Treeview List\JID Treeview List\States.txt

    where the file is currently located in the Solution Explorer.  

    This is when I run the program in the VB2012Express window.

    I would like to have the file installed along with everything else when it gets installed on other machines and to be able to read the file at that location.



    Allen Johnson

    Thursday, November 21, 2013 5:33 PM

All replies

  • When you are debugging, Application.ExecutablePath will return the first path that you listed, as it builds into the bin\Debug folder by default. Even if you did have your text file in the build folder, you would still need to create an installation when you ship the application in order to get the text file installed.

    As an alternative, you may want to consider adding the text file as a project resource. If the text file is not expected to be changed by the user once the application is installed you could specify it as an embedded resource. There is more information here regarding adding files as resources.

    Good luck!

    Thursday, November 21, 2013 5:49 PM
  • I went to the link mentioned and tried this:

    Imports System.Reflection

    Dim _assembly As [Assembly] = [Assembly].GetExecutingAssembly()
            Dim SR As StreamReader
            Try
                SR = New StreamReader(_assembly.GetManifestResourceStream("JIDTreeViewList.States.csv"))
            Catch ex As Exception       

               MessageBox.Show("Resource wasn't found!", "Error")
             End Try

    But it errors out. The file is called "States.csv" and the Rootnamespace is JIDTreeViewList.


    Allen Johnson

    Thursday, November 21, 2013 8:09 PM