应用程序资源中文件的文件路径是什么? [英] What is the file path, as a string, of a file in the application's resources?

查看:46
本文介绍了应用程序资源中文件的文件路径是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的原因是我想在运行时打印应用程序资源中的文件,如下所示:

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 

当我使用 "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf" 作为 FileName 的参数时,它可以工作.但是当我使用 My.Resources.Countdown_Timer_Help 时,它说找不到文件.

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.

推荐答案

不,您没有得到它,该文件只会出现在您的开发机器上.部署程序后,该文件将嵌入您的程序中且无法打印.您必须编写将文件从资源保存到磁盘的代码,然后将其打印出来.例如:

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

鉴于您必须将资源保存到文件中,简单地将文件与您的应用程序一起部署,而不是将其嵌入为资源并一遍又一遍地将其写入磁盘可能更有意义.使用 Application.ExecutablePath 来定位文件.要使其在调试时工作,您必须将文件复制到 bin\Debug.为此,请使用 Project + Add Existing Item 将文件添加到您的项目,并将 Copy to Output Directory 属性设置为 Copy if Newer.

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.

这篇关于应用程序资源中文件的文件路径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆