如何在 .NET 程序集中嵌入文本文件? [英] How to embed a text file in a .NET assembly?
问题描述
我想在程序集中嵌入一个文本文件,这样我就可以加载文本而不必从磁盘读取它,并且我需要的所有内容都包含在 exe 中.(所以它更便携)
I would like to embed a text file in an assembly so that I can load the text without having to read it from disk, and so that everything I need is contained within the exe. (So that it's more portable)
有没有办法做到这一点?我假设与资源文件有关?
Is there a way to do this? I assume something with the resource files?
如果可以的话,你是怎么做的以及你如何以编程方式将文本加载到字符串中?
And if you can, how do you do it and how do you programaticaly load the text into a string?
推荐答案
右键单击项目文件,选择属性".
Right-click the project file, select Properties.
在打开的窗口中,转到资源"选项卡,如果该选项卡页面中间只有一个蓝色链接,请单击它以创建新资源.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
然后在标签页上方的工具栏中,选择添加一个新的文本文件,给它一个名字,它就会被添加到你的项目中并打开.
Then from the toolbar above the tab-page, select to add a new text file, give it a name, it will be added to your project and opened up.
如果你做到了这一点,那么你可以在你的代码中输入 Resources.TheNameYouGaveTheTextFileHere 并且你可以访问它的内容.注意第一次在类中使用Resources类,需要添加using指令(输入Resources后按Ctrl+.获取菜单让VS为你做).
If you get this far, then in your code you can type in Resources.TheNameYouGaveTheTextFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
如果上面的描述有什么不清楚的地方,请留下评论,我会编辑它直到它完成或有意义:)
If something was unclear about the above description, please leave a comment and I'll edit it until it is complete or makes sense :)
这篇关于如何在 .NET 程序集中嵌入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!