vb.net 如何在VB.net中指定相对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25195445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:03:15  来源:igfitidea点击:

How do I specify relative file path in VB.net

vb.netvisual-studiofilerelative-pathfilepath

提问by user155312

I have a Webbrowsercontrol in a form which displays a pdffile. I have to specify the URL as the file location on my computer.

我有一个Webbrowser显示pdf文件的表单控件。我必须将 URL 指定为我计算机上的文件位置。

eg.

例如。

 E:\Folder\Manual.pdf

Both the pdffile and the program are in the same folder.

这两个pdf文件和程序都在同一个文件夹中。

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

如何指定 URL,以便当我将文件夹移动到另一个驱动器时,它会打开相同的 pdf 文件?

回答by Nizam

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)

回答by OneFineDay

If I understand you correctly, then:

如果我理解正确,那么:

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

回答by Yoshi Askharoun

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.

这将在 Windows Media Player ActiveX 对象中播放文件。可以使用 OpenFile 对话框选择文件,在本例中称为 LocateFamilyDialog。您不需要语句的 ElseIf 部分,但您需要插入一个打开的文件对话框和一个可以显示 PDF 的控件。我认为它可以与 WebBrowsers 一起使用,但我不确定。