【问题标题】:What is the file path, as a string, of a file in the application's resources?应用程序资源中文件的文件路径(作为字符串)是什么?
【发布时间】:2011-01-08 13:23:39
【问题描述】:

我问的原因是我想在运行时打印应用程序资源中的一个文件,如下所示:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim printProcess As New Process
printProcess.StartInfo.CreateNoWindow = True
printProcess.StartInfo.FileName = "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf"
printProcess.StartInfo.FileName = My.Resources.Countdown_Timer_Help
printProcess.StartInfo.Verb = "Print"
printProcess.Start()
End Sub 

当我使用“C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf”作为 FileName 的参数时,它可以工作。但是当我使用 My.Resources.Countdown_Timer_Help 时,它说找不到文件。

【问题讨论】:

    标签: vb.net printing resources


    【解决方案1】:

    好的,我明白了。

    System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\") & "Countdown_Timer_Help.rtf"
    

    【讨论】:

    • 这是获取 bin/debug.... 的路径,这意味着您将两个文件夹返回到主项目路径并访问资源。因此,当您部署应用程序时,资源路径将不存在。通过将应用程序 EXE 复制到另一个位置进行测试...
    • 我知道这是旧的,但您需要将 BRACKET 移到语句的末尾 ... rtf")
    【解决方案2】:

    不,您没有得到它,该文件只会出现在您的开发机器上。部署程序后,该文件将嵌入到您的程序中并且无法打印。您必须编写将文件从资源保存到磁盘的代码,然后将其打印出来。例如:

      Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim path As String = Application.UserAppDataPath
        path = System.IO.Path.Combine(path, "Help.rtf")
        System.IO.File.WriteAllText(path, My.Resources.Countdown_Timer_Help)
        Dim printProcess As New Process
        printProcess.StartInfo.CreateNoWindow = True
        printProcess.StartInfo.FileName = path
        printProcess.StartInfo.Verb = "Print"
        printProcess.Start()
      End Sub
    

    鉴于您必须将资源保存到文件中,将文件与您的应用一起部署可能更有意义,而不是将其作为资源嵌入并一遍又一遍地写入磁盘。使用 Application.ExecutablePath 来定位文件。要在调试时进行这项工作,您必须将文件复制到 bin\Debug。为此,请使用 Project + Add Existing Item 将文件添加到您的项目,并将 Copy to Output Directory 属性设置为 Copy if Newer。

    【讨论】:

    • 谢谢。我将首先必须获得另一台计算机或在外部硬盘驱动器上安装 Windows 以进行测试。
    • Erm,只需将它安装在您的开发机器上。它不会安装到您的项目目录中。
    猜你喜欢
    • 2018-12-19
    • 2011-08-25
    • 2012-09-16
    • 1970-01-01
    • 2011-03-07
    • 2011-03-27
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多