views:

72

answers:

2

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.

A: 

OK, I got it.

System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\") & "Countdown_Timer_Help.rtf"
Geoffrey Van Wyk
+1  A: 

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.

Hans Passant
Thanks. I will first have to get another computer or install windows on an external hard drive, to test it on.
Geoffrey Van Wyk
Erm, just install it on your dev machine. It won't install to your project directory.
Hans Passant