如何使用AssemblyBuilder设置exe图标?

发布于 2024-08-25 10:59:26 字数 375 浏览 8 评论 0 原文

鉴于我正在使用 AssemblyBuilder 生成 exe 应用程序,如何为其设置图标

我想我应该使用

System.Reflection.Emit.AssemblyBuilder.DefineUnmanagedResource

有一个例子如何做到这一点吗?


http://msdn.microsoft.com/en-us /library/aa380599(VS.85).aspx

Given that I am generating an exe application with AssemblyBuilder, how do I set an icon to it?

I think I should be using

System.Reflection.Emit.AssemblyBuilder.DefineUnmanagedResource

Is there an example how to do it?


http://msdn.microsoft.com/en-us/library/aa380599(VS.85).aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も星光 2024-09-01 10:59:26

您还可以在保存 EXE 后使用 Win32 资源 API BeginUpdateResource、UpdateResource 和 EndUpdateResource 更改图标。请参阅从代码更改 WPF 程序集图标(这不是 WPF 特定的)。

You could also change the icon after saving the EXE using the Win32 resource APIs BeginUpdateResource, UpdateResource and EndUpdateResource. See Change WPF Assembly Icon from Code (which isn't WPF specific).

心如狂蝶 2024-09-01 10:59:26

是的,您需要 DefineUnmanagedResource()。您传递的文件必须采用 .RES 文件格式。这需要 rc.exe Windows SDK 工具。要创建一个,首先创建一个名为 test.rc 的文本文件,其中包含以下内容:

 100 ICON test.ico

其中 test.ico 是包含该图标的文件的名称。启动 Visual Studio 命令提示符并使用此命令

 rc test.rc

创建 test.res 文件。将其路径传递给 DefineUnmanagedResource(),最终的 .exe 包含图标资源。

请注意,这并不实用,目标计算机可能没有安装 Windows SDK,并且您无法重新分发 rc.exe。但您可以分发 .res 文件。

Yes, you'll need DefineUnmanagedResource(). The file you pass must be in the .RES file format. That requires the rc.exe Windows SDK tool. To create one, start by creating a text file named test.rc with this content:

 100 ICON test.ico

Where test.ico is the name of a file that contains the icon. Start the Visual Studio Command Prompt and use this command

 rc test.rc

That creates a test.res file. Pass its path to DefineUnmanagedResource(), your final .exe contains the icon resource.

Note that this is not verify practical, the target machine probably won't have the Windows SDK installed and you cannot redistribute rc.exe. But you could distribute the .res file.

去了角落 2024-09-01 10:59:26

您需要通过定义 IResourceWriter 来设置 ResourceManager 并写入它,然后根据 MSDN,我想代码看起来像这样,因为根据代码示例判断,我以前没有这样做过,保存程序集后,添加非托管资源并将其命名为“app.ico”:

// Defines a standalone managed resource for this assembly.
IResourceWriter myResourceWriter = myAssembly.DefineResource("myResourceIcon",
         "myResourceIcon.ico", "MyAssemblyResource.resources", 
         ResourceAttributes.Public);
myResourceWriter.AddResource("app.ico", "Testing for the added resource");

myAssembly.Save(myAssembly.GetName().Name + ".dll");

// Defines an unmanaged resource file for this assembly.
bool bSuccess = false;
byte[] iconB = null;
using (System.IO.FileStream fStream = new FileStream("icon.ico", FileMode.Open, FileAccess.Read)){
    iconB = new byte[(int)fStream.Length];
    int nRead = fStream.Read(out iconB, 0, iconB.Length);
    if (nRead == iconB.Length) bSuccess = true;
}
if (bSuccess){
    myAssembly.DefineUnmanagedResource(iconB);
}

You need to set up an ResourceManager by defining an IResourceWriter and write to it, then read in the icon as bytes and set it, according to this documentation from MSDN, I guess the code would look something like this, as I have not done this before, judging by the code sample, after you save the assembly, add the unmanaged resource and name it as 'app.ico':

// Defines a standalone managed resource for this assembly.
IResourceWriter myResourceWriter = myAssembly.DefineResource("myResourceIcon",
         "myResourceIcon.ico", "MyAssemblyResource.resources", 
         ResourceAttributes.Public);
myResourceWriter.AddResource("app.ico", "Testing for the added resource");

myAssembly.Save(myAssembly.GetName().Name + ".dll");

// Defines an unmanaged resource file for this assembly.
bool bSuccess = false;
byte[] iconB = null;
using (System.IO.FileStream fStream = new FileStream("icon.ico", FileMode.Open, FileAccess.Read)){
    iconB = new byte[(int)fStream.Length];
    int nRead = fStream.Read(out iconB, 0, iconB.Length);
    if (nRead == iconB.Length) bSuccess = true;
}
if (bSuccess){
    myAssembly.DefineUnmanagedResource(iconB);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文