开发者

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

开发者 https://www.devze.com 2022-12-17 02:53 出处:网络
The reason I ask is that I want to print, at run-time, a file in the application\'s resources, like this:

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.R开发者_运维问答esources.Countdown_Timer_Help, it says it cannot find the file.


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.


OK, I got it.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号