C# 如何在 .NET 程序集中嵌入文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/433171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed a text file in a .NET assembly?
提问by Adam Haile
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)
我想在程序集中嵌入一个文本文件,以便我可以加载文本而不必从磁盘读取它,并且我需要的所有内容都包含在 exe 中。(所以它更便携)
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?
如果可以的话,你是怎么做的,你如何以编程方式将文本加载到字符串中?
采纳答案by Lasse V. Karlsen
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.
然后从标签页上方的工具栏中,选择添加一个新的文本文件,为其命名,它将被添加到您的项目中并打开。
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).
如果你做到了这一点,那么你可以在你的代码中输入 Resources.TheNameYouGaveTheTextFileHere 并且你可以访问它的内容。请注意,第一次在类中使用 Resources 类时,需要添加 using 指令(在键入 Resources 后按 Ctrl+. 获取菜单让 VS 为您执行此操作)。
If something was unclear about the above description, please leave a comment and I'll edit it until it is complete or makes sense :)
如果以上描述有不清楚的地方,请发表评论,我将对其进行编辑,直到完成或有意义:)
回答by Otávio Décio
Yes, you are correct - create a resource file. WHen you do that you don't need to "load" the string, it will be referenced as Resource.WhateverStringYouDefined.
是的,你是对的 - 创建一个资源文件。当您这样做时,您不需要“加载”字符串,它将被引用为 Resource.WhateverStringYouDefined。
回答by Drew Noakes
In Visual Studio 2003, Visual Studio 2005 and possibly earlier versions (this works in 2008 as well) you can include the text file in your project, then in the 'Properties' panel, set the action to 'Embedded Resource'. Then you can access the file as a stream using Assembly.GetManifestResourceStream(string)
.
在 Visual Studio 2003、Visual Studio 2005 和可能的早期版本(这也适用于 2008 年)中,您可以在项目中包含文本文件,然后在“属性”面板中,将操作设置为“嵌入资源”。然后,您可以使用Assembly.GetManifestResourceStream(string)
.
Other answers here are more convenient. I include this for completeness.
这里的其他答案更方便。我包括这个是为了完整性。
Note that this approach will work for embedding other types of files such as images, icons, sounds, etc...
请注意,此方法适用于嵌入其他类型的文件,例如图像、图标、声音等...
回答by Narottam Goyal
After embeding a text file, use that file any where in code like this...
嵌入文本文件后,在代码中的任何位置使用该文件,如下所示......
global::packageName.Properties.Resources.ThatFileName
回答by Pavan
Here is what I did:
这是我所做的:
- Added my files (resources) in Visual Studio by right-clicking on the project.
- Right click on every file you have added and change the "Build Type" to Embedded Resource.
In order to access the resource:
a. Got the current assembly using the function:
GetExecutingAssembly()
b. The resource that I added was a text file so I read it into a stream using
GetManifestResourceStream(fileName)
. The way I accessed the file names is by callingGetManifestResourceNames()
c. Now use a
StreamReader()
class to read to the end of file into a variable if that is what you want.
- 通过右键单击项目在 Visual Studio 中添加我的文件(资源)。
- 右键单击您添加的每个文件并将“构建类型”更改为嵌入式资源。
为了访问资源:
一种。使用以下函数获取当前程序集:
GetExecutingAssembly()
湾 我添加的资源是一个文本文件,因此我使用
GetManifestResourceStream(fileName)
. 我访问文件名的方式是通过调用GetManifestResourceNames()
C。现在使用一个
StreamReader()
类将文件末尾读入一个变量,如果这是你想要的。
回答by Narjis Hasan
Adding to Pavan's answer, to get the current assembly(in general section):
添加到 Pavan 的答案,以获取当前程序集(在一般部分):
Assembly _assembly;
GetManifestResourceStream(fileName)(in code, where the read from resource is required):
GetManifestResourceStream(fileName)(在代码中,需要从资源读取):
try
{
_assembly = Assembly.GetExecutingAssembly();
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("*Namespace*.*FileName*.txt"));
}
catch
{
Console.WritelLine("Error accessing resource!");
}
回答by c00000fd
Here's what worked for me. (I needed to read contents of a file embedded into an executable .NET image file.)
这对我有用。(我需要读取嵌入到可执行 .NET 图像文件中的文件的内容。)
Before doing anything, include your file into your solution in Visual Studio. (In my case VS 2017 Community.) I switched to the Solution Explorer
, then right-clicked Properties
folder, chose Add Existing Item
and picked the file. (Say, FileName.txt
.) Then while still in the Solution Explorer
, right-click on the included file, select Properties
, and pick Build Action
as Embedded Resource
.
在执行任何操作之前,请将您的文件包含在 Visual Studio 中的解决方案中。(在我的情况下是 VS 2017 社区。)我切换到Solution Explorer
,然后右键单击Properties
文件夹,选择Add Existing Item
并选择文件。(比如说,FileName.txt
。)然后在 中Solution Explorer
,右键单击包含的文件,选择Properties
,然后选择Build Action
为Embedded Resource
。
Then use this code to read its bytes:
然后使用此代码读取其字节:
string strResourceName = "FileName.txt";
Assembly asm = Assembly.GetExecutingAssembly();
using( Stream rsrcStream = asm.GetManifestResourceStream(asm.GetName().Name + ".Properties." + strResourceName))
{
using (StreamReader sRdr = new StreamReader(rsrcStream))
{
//For instance, gets it as text
string strTxt = sRdr.ReadToEnd();
}
}
Note, that in this case you do notneed to add that file as a resource as was proposed in the accepted answer.
请注意,在这种情况下,您不需要将该文件添加为已接受答案中建议的资源。