在记事本中打开文本文件格式的资源
我在使用资源文件时遇到一些问题。
在我的项目中,我使用了一堆位图文件和两个 txt 文件。位图用作控件的背景,但我想在记事本或写字板中打开 .txt 文件。 到目前为止,我从计算机上的文件路径访问它们:
ControlName.backgroundimage = New Bitmap(direcorypath & "/Map.bmp")
Process.Start(direcorypath & "/ Instrukcje.txt")
而且效果很好。现在我将这些文件添加到我的资源中(将它们放在可执行文件中对我来说很实用) 并将这些行替换为:
ControlName.backgroundimage = New Bitmap(MyProject.My.Resources.Map)
Process.Start(MyProject.My.Resources.Instrukcje)
对于位图工作正常,但对于 txt 文件,会出现错误,提示找不到文件。 有趣的是:当我在“Watch”中输入“MyProject.My.Resources.Instrukcje”时,它会显示文件的内容。那么它就在那里,只是不是作为一个文件?我想我在这里遗漏了一些东西
我到处搜索这是什么东西,但找不到任何有用的东西。我将不胜感激一些线索。 (抱歉,如果有不清楚的地方,我的编程能力比我不太好的英语差得多) 谢谢:)
I'm having some trouble using resource files.
In my project I'm using a bunch of bitmap files and two txt files. Bitmaps are used as backgrounds for controls, but .txt files I want to open in Notepad or WordPad.
Till now I was accesing them from a file path on my machine:
ControlName.backgroundimage = New Bitmap(direcorypath & "/Map.bmp")
Process.Start(direcorypath & "/Instrukcje.txt")
And it worked fine. Now I added these files to my resources (it's practical for me to have them in executable file)
And replaced those lines with:
ControlName.backgroundimage = New Bitmap(MyProject.My.Resources.Map)
Process.Start(MyProject.My.Resources.Instrukcje)
For bitmap it works fine, but for txt file an error occurs saying that a file cannot be found.
Interestin thing is: when I type "MyProject.My.Resources.Instrukcje" in "Watch" it shows the content of the file. So it is there, only not as a file? I think I am missing something here
I searched everywhere whats the thing, but couldn't find anything helpful. I will be gratefil for some clues.
(And sorry if somethings unclear, my programming skills are much worse than my not-so-good English)
Thanks:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
资源嵌入在 .dll 或 .exe 文件中,因此无法使用 Process 类进行访问。您可以将 .txt 资源写入临时目录,并使用 Process 在那里启动临时文件。
附加信息:我已将 .txt 文件添加到 Resource.resx 文件来测试您的问题,并且纯文本作为字符串存储在资源文件中。因此,如果访问 Resources.(InsertYourTextFilesName) 属性,则会返回文本文件中的文本。
如果您可能想知道:二进制文件(例如非纯文本文件)作为字节数组存储在resource.resx中,并在编译后的程序集中驻留。
Resources are embedded in your .dll or .exe file and therefore cannot be accessed with the Process class. You could write the .txt resource to a temp-directory and use Process to start the temp file there.
Additional info: I've added a .txt file to a Resource.resx file to test your question, and plain text is stored as a string in the resource file. Therefore, if you access the Resources.(InsertYourTextFilesName) property, the text within the textfile is returned.
In case you might want to know: binary files (e.g. non-plain-text files) are stored as byte array in the resource.resx and interned within the compiled assembly as such.