sys*_*ich 2 vb.net printing resources
我问的原因是我想在运行时打印应用程序资源中的文件,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
当我使用"C:\ Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf"作为FileName的参数时,它可以工作.但是当我使用My.Resources.Countdown_Timer_Help时,它说它无法找到该文件.
不,你没有得到它,该文件只会出现在您的开发机器上.部署程序后,该文件将嵌入您的程序中,无法打印.您必须编写将文件从资源保存到磁盘的代码,然后将其打印出来.例如:
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
Run Code Online (Sandbox Code Playgroud)
鉴于您必须将资源保存到文件,简单地使用您的应用程序部署文件而不是将其作为资源嵌入并将其一遍又一遍地写入磁盘可能更有意义.使用Application.ExecutablePath查找文件.要在调试时完成这项工作,您必须将文件复制到bin\Debug.通过使用Project + Add Existing Item将文件添加到项目并将Copy to Output Directory属性设置为Copy by Newer来实现.