How do I specify relative file path in VB.net
Asked Answered
C

3

6

I have a Webbrowser control in a form which displays a pdf file. I have to specify the URL as the file location on my computer.

eg.

 E:\Folder\Manual.pdf

Both the pdf file and the program are in the same folder.

How do I specify the URL so that when I move the folder onto another drive, it opens the same pdf file?

Clothespress answered 8/8, 2014 at 2:59 Comment(1)
Just use the fully qualified path in the url. It doesn't matter where it is then in relation to the exeDream
A
12

The location of your application is

 Dim path as String = My.Application.Info.DirectoryPath 

The you could use:

Dim pdffile as String = IO.Path.Combine(path, "pdffile.pdf")
WebBrowser1.Navigate(pdffile)
Ageold answered 8/8, 2014 at 4:12 Comment(1)
Please add more details how does this help.Haerle
B
3

If I understand you correctly, then:

Dim myPdf As String = 
    IO.Path.Combine(IO.Directory.GetParent(Application.ExecutablePath).FullName, "myPdfFile.pdf")
Blandishment answered 8/8, 2014 at 4:9 Comment(1)
I'm not the downvoter, but you're missing to import any library so Directory is not recognized by default frameworkFarah
F
1

Another way you could do it is by using something like the code below;

Private Sub FamilyLocateFile_Click(sender As Object, e As EventArgs) Handles FamilyLocateFile.Click
    If LocateFamilyDialog.ShowDialog = DialogResult.OK Then
        FamilyWMP.URL = LocateFamilyDialog.FileName
    ElseIf LocateFamilyDialog.ShowDialog = DialogResult.Cancel Then
        MsgBox(MsgBoxStyle.Critical, "Error!")
    End If
End Sub

What this will do is play a file in a Windows Media Player ActiveX object. The file can be selected with an OpenFile Dialog, which in this case is called LocateFamilyDialog. You don't need the ElseIf part of the statement, but you will need to insert an open file dialog and a control that can display PDFs. I think it'll work with WebBrowsers, but I'm not sure.

Franciscka answered 21/4, 2016 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.