简体   繁体   English

在Visual Studio中向Windows资源添加图标

[英]Add icon to Windows resources in Visual Studio

I am trying to add an icon to an .NET exe built in the Visual Studio, but somehow it does not work. 我试图将图标添加到Visual Studio中内置的.NET exe中,但是以某种方式无法正常工作。

Here are steps to reproduce the problem: 1. I created a new Windows Form Application application: New Project -> Windows Form Application 2. Open the resources in it: Right click on Solution -> Properties -> Resources 3. Tried to add an icon file to it: Icons -> Add Resources -> Add Exising File-> and chose an existing icon. 重现此问题的步骤如下:1.我创建了一个新的Windows窗体应用程序应用程序:New Project-> Windows Form Application 2.在其中打开资源:右键单击Solution-> Properties-> Resources 3.尝试添加一个图标文件:图标->添加资源->添加现有文件->并选择一个现有图标。

It appeared that the icon was added. 似乎已添加图标。 So, I build the exe file and closed the solution, but the resulting exe file did not contain the icon in the Windows resources. 因此,我生成了exe文件并关闭了解决方案,但是生成的exe文件在Windows资源中不包含图标。 I verified that by opening the exe file by Visual Studio. 我通过Visual Studio打开exe文件验证了这一点。 The Visual Studio showed resources to me, which contained the manifest and version, but no icon. Visual Studio向我显示了资源,其中包含清单和版本,但没有图标。

The question: what am I doing wrong? 问题:我在做什么错? Why does the added icon not appear in the Windows resources? 为什么添加的图标没有出现在Windows资源中?

Do the resources show up in your solution explorer? 资源会显示在您的解决方案资源管理器中吗? If not, I would check here and follow those steps. 如果没有,我会在这里检查并按照以下步骤操作。 Additionally, to check that the resource is in fact added, you can add something like this 此外,要检查是否确实添加了资源,您可以添加如下内容

MessageBox.Show(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames().Aggregate((x,y)=> x + " " + y ));

Note that for this to show your resource, you have to navigate to the resource in solution explorer, open properties on it, and select Build Action -> Embedded Resource. 请注意,要显示资源,您必须在解决方案资源管理器中导航到该资源,在其上打开属性,然后选择“构建操作”->“嵌入式资源”。 (You can also use Resource, see article here for the difference between the two options) (您也可以使用资源,请参阅此处的文章了解两个选项之间的区别)

As a final note, after I did this, I tried using your procedure to open the .exe in Visual Studio, and I was indeed unable to find the resources in the view. 最后,做完这些之后,我尝试使用您的过程在Visual Studio中打开.exe,但实际上我无法在视图中找到资源。 However opening up the same exe in dotPeek, I was able to find the whole image I used embedded in the code, under Resources. 但是,在dotPeek中打开相同的exe时,我能够在参考资料下找到代码中嵌入的整个图像。

Managed resources are embedded into assemblies in a different way from Win32 resources. 托管资源以与Win32资源不同的方式嵌入到程序集中。

If the goal is just to add the default icon, it can be done using the "Application" tab in the Project Properties. 如果目标只是添加默认图标,则可以使用项目属性中的“应用程序”选项卡来完成。

If the goal add arbitrary Win32 resources then the answer can be found here: How to embed a resource in a .NET PE executable? 如果目标添加了任意Win32资源,则可以在此处找到答案: 如何在.NET PE可执行文件中嵌入资源?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM