Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path of resource file?

Tags:

.net

path

vb.net

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")
like image 244
winter Avatar asked Oct 24 '25 17:10

winter


1 Answers

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
like image 141
Ibrahim D. Avatar answered Oct 26 '25 09:10

Ibrahim D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!