What is the file path, as a string, of a file in the application's resources?

≡放荡痞女 提交于 2019-12-24 02:05:37

问题


The reason I ask is that I want to print, at run-time, a file in the application's resources, like this:

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 

When I use "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf" as the argument of FileName, it works. But when I use My.Resources.Countdown_Timer_Help, it says it cannot find the file.


回答1:


No you didn't get it, that file will only be present on your dev machine. After you deploy your program, the file will be embedded in your program and cannot be printed. You'll have to write the code that saves the file from the resource to disk, then prints it. For example:

  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

Given that you have to save the resource to a file, it probably makes more sense to simply deploy the file with your app instead of embedding it as a resource and writing it to disk over and over again. Use Application.ExecutablePath to locate the file. To make that work at debug time you have to copy the file to bin\Debug. Do so by adding the file to your project with Project + Add Existing Item and setting the Copy to Output Directory property to Copy if Newer.




回答2:


OK, I got it.

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


来源:https://stackoverflow.com/questions/2123257/what-is-the-file-path-as-a-string-of-a-file-in-the-applications-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!