What is the Correct "File path" for a text file that is displayed in a project?

VKSB 236 Reputation points
2021-01-04T12:47:59.517+00:00

Dear Friends,

In my Project, I want to display a text file in a "TextBox" when "Help" button is clicked.

My code is given below.

 Private Sub TwilightDefinitionsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TwilightDefinitionsToolStripMenuItem.Click

        Dim TextFile2 As String = ""

        Twilight_DefinitionDialog.Show()


        'TextFile2 = My.Computer.FileSystem.ReadAllText("C:\Users\VKSBK\Desktop\My Project \Trial Project\Twilight Definitions.txt")

        TextFile2 = My.Computer.FileSystem.ReadAllText("C:\Program Files (x86)\VKS\Trial Project\Twilight Definitions.txt")

        Twilight_DefinitionDialog.TwilightDefitionDialog_TxtBox.Text = (TextFile2)

    End Sub

This works fine when I click "Start Debug". Then I made a setup file and installed it on "another" computer; clicking the button gave me an error message:

"Could not find a part of the path "C:\Program Files (x86)\VKS\Trial Project\Twilight Definitions.txt".

I think "\VKS\" part is the problem. I couldn't find out how to rectify it.

Could you please let me know how to rectify this error & let me know the "Correct" way to give the "File path" so that it works on any user computer.

Thanks
Kind regards
VKSBK

Developer technologies VB
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-01-04T13:05:30.157+00:00

    Clarify which folder is used by setup application. If “Twilight Definitions.txt” is installed into your application folder, then try this code:

    Imports System.IO
    
    . . .
    Dim p = Path.Combine(Application.StartupPath, "Twilight Definitions.txt")
    MsgBox(p)
    TextFile2 = File.ReadAllText(p)
    

    MsgBox is to check the path. Remove it later.

    0 comments No comments

  2. VKSB 236 Reputation points
    2021-01-04T13:54:17.93+00:00

    Dear Viorel-1,

    Thanks for the reply.

    Sorry, I don't know the answer for "Clarify which folder is used by setup application. If “Twilight Definitions.txt” is installed into your application folder, ".

    I give "VKS" for the "Manufacturer" of the "Setup Deployment Project Properties; and keep the "Twilight Definitions.txt" in the "Application Folder" under "File System on Target Machine"; shown in the screenshot.
    53324-capture1.png

    So, when I install this program on my computer, a folder named "VKS" is created in the "C:\Program Files (x86)". The "Twilight Definitions.txt" is installed in the folder "VKS".

    I hope I have answered your question; please correct if I am wrong.

    I want to know if I give the path of the "File" as you mentioned above, will it work while I am checking the project (before I create a Setup file and Install on any computer.

    I also have a picture ( a PNG file) in this project that explain the "Twilight Definitions"; this caused some problem when I was checking the project, so I added that picture file to the "Resources" (In the Menu ---> "Project" ---> "Trial Project Properties" ---> "Resources"). After adding to the Resources the Picture is displayed without any error.

    But, the picture also gives error message after the "Trial Project" is installed on different computer.
    So I understand the path for the picture file too should be similar to what you mentioned.

    Please let me know what this "Resources" is meant for?

    Thanks
    Kind Regards
    VKSBK

    0 comments No comments

  3. Michael Taylor 60,161 Reputation points
    2021-01-04T15:44:12.09+00:00

    If the file you're trying to open is in the same directory as your application then you don't need a path. Just put in the filename. Windows will search the current working directory (along with a few others) when no path is specified. By default the working directory is where the app is.

    MyApp.exe
    MyFile.txt
    DataFiles\MyData.dat

       File.Open("MyFile.txt")  // No path, use app path first  
    

    Alternatively use relative paths

       File.Open(@".\MyFile.txt")     //Look in working directory  
       File.Open(@"DataFiles\\MyData.dat")  //Look in subfolder  
    

    You only need to specify absolute paths if the file you need is outside the app's directory (which is generally a bad idea). In this case you cannot hard code a path as paths are not fixed in Windows. To get a well defined system path (such as Program FIles) use Environment.GetFolderPath.

       //Returns C:\Program Files (x86)\VKS on many Windows systems  
       var dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86), "VKS");  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.