winter
winter

Reputation: 237

How to get path of resource file?

I am making a project in Visual Basic and I have a control that must open a file. I have added this file inside the Resources folder of the project, and if I drag the file into the code I get the full path, but of course that is the full path in my computer and will not work on any other computer.

It is help file, this is the code:

Help.ShowHelp(Me, "help.chm")

How can I get the relative path so my project will work correctly on any computer? I've tried things like the following, which do not work:

Help.ShowHelp(Me, "..\help.chm")
Help.ShowHelp(Me, "..\\help.chm")
Help.ShowHelp(Me, "..\\Resources\help.chm"")

Edit: I fixed the problem by simply adding the file to the \bin folder. Then this line worked just fine:

Help.ShowHelp(Me, "help.chm")

Upvotes: 4

Views: 8157

Answers (2)

Ibrahim D.
Ibrahim D.

Reputation: 326

First I would recommend when you add a file to the Resource section of your project is to do it from the My Project > Resources section

enter image description here

enter image description here

Now for the code: To access your file in the Resources of the project, you can it from My.Resources

But keep in mind, that this will give you a bytes array and not the file location. What you will need to do is to save this bytes array to a file and then use this file in your project.

Example:

'A FileInfo object that will hold your help file FilePath
Dim helpFileLoc As FileInfo
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Access our help file from the Resources section and store the file as bytesarray
    Dim helpflbytes() As Byte = My.Resources.Application_form
    'Save the bytesarray to a real file (you can change the save path)
    File.WriteAllBytes(Application.StartupPath & "\appform.doc", helpflbytes)
    'Create a fileinfo object that will have the FilePath of your help file
    helpFileLoc = New FileInfo(Application.StartupPath & "\appform.doc")
End Sub

'When user clicks a button, your app will open your help file
Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click
    'Since helpFileLoc, the FileInfo object contains the path to your help file, you can start it as a process
    Process.Start(helpFileLoc.FullName)
End Sub

Upvotes: 2

Rohan Bari
Rohan Bari

Reputation: 7726

From the question, it seems like you're trying to get the resource path dynamically which should work for everyone's computer. You can do it with several popular methods:

1. Use Environment.GetEnvironmentVariable() to get relative paths in any Windows system.

Suppose your application saves the data into %temp% path, then you can access it as follows:

Dim resourcePath As String = Environment.GetEnvironmentVariable("TEMP").ToString + "\Resources\help.chm"

Alternatively, you can use My.Computer.Filesystem.SpecialDirectories.Temp too instead of getting the environment variable.

2. suppose your application is saving a file in its corresponding path, then you may use the following code to get the full path:

Dim resourcePath As String = My.Application.Info.DirectoryPath.ToString + "\Resources\help.chm```

Upvotes: 0

Related Questions