简体   繁体   中英

How do I specify relative file path in VB.net

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?

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)

If I understand you correctly, then:

Dim myPdf As String = 
    IO.Path.Combine(IO.Directory.GetParent(Application.ExecutablePath).FullName, "myPdfFile.pdf")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM