如何打开位于winform项目内的文件

时间:2013-08-30 18:43:40

标签: vb.net winforms

我有一个项目需要从我的表单中打开一些pdf报告。由于程序可以安装在任何驱动器中,我使用winform位置目录作为我的路径。我的报告位于名为Reports的目录中。它在开发下运行良好,因为我的winform在bin \ debug下,但是当我部署我的解决方案并尝试该过程时,它无效,无法找到该文件。我在部署后创建了一个Reports目录,但仍未找到我的pdf。

以下是代码:

        'Open the requested file name. The files are stored in the
    'same path as the main workbook.
    '@param filename  file to be opened

    Function OpenReports(strFileName As String) As String

        Dim reportFolder As String

        Try

            reportFolder = Path.Combine(Directory.GetCurrentDirectory(), "Reports")
            System.Diagnostics.Process.Start(reportFolder & "\" & strFileName)

        Catch ex As Exception

            MsgBox("The " & strFileName & " is not found on directory")

        End Try

        Return strFileName

    End Function

Private Sub btnRptRangesToMarket_Click(sender As Object, e As EventArgs) Handles btnRptRangesToMarket.Click

    'This procedure runs when the btnRptRangesToMarket is clicked
    'the procedure opens the pdf below by running the OpenReports Function

    OpenReports("Ranges to Market.pdf")

End Sub

1 个答案:

答案 0 :(得分:2)

尝试使用Application.StartupPath代替Directory.GetCurrentDirectory()。我总是用这个属性获得好成绩。

这是来自MSDN:

当前目录与原始目录不同,原始目录是启动进程的目录。