Geoffrey
Geoffrey

Reputation: 5432

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

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.

Upvotes: 2

Views: 10811

Answers (2)

Hans Passant
Hans Passant

Reputation: 942308

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.

Upvotes: 7

Geoffrey
Geoffrey

Reputation: 5432

OK, I got it.

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

Upvotes: 2

Related Questions