C# 如何将所有文件编译为一个exe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11862532/
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 compile all files to one exe?
提问by Segev
I wrote a project in C# that uses a lot of images, Milk models and openGL and i want to pack everything in one exe so i can upload it to my site. Right now i got an exe that is depended on other files like jpgs etc'. I've tried using ILMerge but couldn't get it to work. Is there a simpler solution? thanks.
我用 C# 编写了一个项目,该项目使用了大量图像、Milk 模型和 openGL,我想将所有内容打包到一个 exe 中,以便我可以将其上传到我的网站。现在我得到了一个依赖于其他文件(如 jpgs 等)的 exe。我试过使用 ILMerge,但无法让它工作。有没有更简单的解决方案?谢谢。
采纳答案by Rajesh
Add that as an embedded resource.
将其添加为嵌入式资源。
Inside Visual Studio :
在 Visual Studio 中:
- Go to Solution Explorer,
- Right click the image,
- GO to Build Actions: Select Embedded Resource.
- 转到解决方案资源管理器,
- 右键单击图像,
- 转到构建操作:选择嵌入式资源。
You will have that image inside the exe. Later you can use Reflection and get the image when you run your application.
您将在 exe 中拥有该图像。稍后您可以使用反射并在运行应用程序时获取图像。
========= Getting the Embedded image from the Application =========
========== 从应用程序中获取嵌入的图像 ==========
First solve the first problem: by putting images as embedded resource.
首先解决第一个问题:将图像作为嵌入资源。
Second problem: Access the images by using Reflection:
第二个问题:使用反射访问图像:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("EmbeddingExample.image1.bmp");
Bitmap image = new Bitmap(myStream);
this.ClientSize = new Size(image.Width, image.Height);
PictureBox pb = new PictureBox();
pb.Image = image;
pb.Dock = DockStyle.Fill;
this.Controls.Add(pb);
}
Borrowed Source Code from here:
从这里借用的源代码:
回答by logicnp
You can put all your files/images into the exe as Embedded Resources.
您可以将所有文件/图像作为Embedded Resources放入 exe 中。
See How to embed and access resources by using Visual C#(This link currently 404s)
请参阅如何使用 Visual C# 嵌入和访问资源(此链接当前为 404s)
回答by PhonicUK
ilmerge is only for merging .net CLR binaries together, usually for bundling libraries into your main executable.
ilmerge 仅用于将 .net CLR 二进制文件合并在一起,通常用于将库捆绑到主可执行文件中。
For things like art assets, you want to embed them as resources into your application. From a resource you can get a stream which lets you work with the data as if it were in a file.
对于艺术资产之类的东西,您希望将它们作为资源嵌入到您的应用程序中。从资源中,您可以获得一个流,让您可以像处理文件一样处理数据。
See this MSDN article for information on embedding resources: http://support.microsoft.com/kb/319292
有关嵌入资源的信息,请参阅此 MSDN 文章:http: //support.microsoft.com/kb/319292
回答by Fedor Hajdu
When you add an image to the project in properties you can set it as Embedded Resource, then it'll be added to the binary file (dll or exe)
当您在属性中将图像添加到项目时,您可以将其设置为嵌入式资源,然后它将被添加到二进制文件(dll 或 exe)中
回答by Tinku
I shall prefer to create a satellite assembly for resource files. http://msdn.microsoft.com/en-us/library/21a15yht%28v=vs.71%29.aspx
我更愿意为资源文件创建一个附属程序集。http://msdn.microsoft.com/en-us/library/21a15yht%28v=vs.71%29.aspx

