读取DLL中的嵌入式资源文件

提取DLL内嵌资源文件
本文介绍了一种从DLL文件中提取嵌入式资源的方法,并提供了详细的C#代码实现。该方法通过加载DLL并获取其资源名称,然后逐一读取资源内容并保存为文件。

读取DLL中的嵌入式资源文件, 以便以静态资源文件的形式布署到站点中.

using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Reflection; namespace 提取资源文件 { class Program { static void Main(string[] args) { //Input Example: //string strConponentPath = @"D:\Documents and Settings\zhengjian\My Documents\Visual Studio 2005\Projects\WindowsApplication1\bin\Debug\WebComponent.dll"; //string strOutputDirectory = @"D:\Documents and Settings\zhengjian\My Documents\Visual Studio 2005\Projects\WindowsApplication1\bin\Debug\Output1\"; string strConponentPath = String.Empty; string strOutputDirectory = String.Empty; Console.Write("请输入程序集文件全名和资源输出路径(必须输入两个路径且用空格隔开):"); string value = (string)System.Console.ReadLine(); value = value.Trim(); if (String.IsNullOrEmpty(value) == true) { Console.Write("输入错误!"); System.Console.ReadLine(); return; } string[] arrPath = value.Split(' '); if (arrPath.Length < 2) { Console.Write("输入错误!"); System.Console.ReadLine(); return; } strConponentPath = arrPath[0]; strOutputDirectory = arrPath[1]; if (String.IsNullOrEmpty(strConponentPath) == true || String.IsNullOrEmpty(strOutputDirectory) == true) { Console.Write("输入错误!"); System.Console.ReadLine(); return; } if (Directory.Exists(strOutputDirectory) == true) { Directory.Delete(strOutputDirectory, true); } Directory.CreateDirectory(strOutputDirectory); string[] resources = Assembly.LoadFile(strConponentPath).GetManifestResourceNames(); foreach (string strResource in resources) { Stream stream = Assembly.LoadFile(strConponentPath).GetManifestResourceStream(strResource); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); //Ufida.T.EAP.Voucher.WebComponent.Control.button-disable.png string strRelativePath = strResource.Replace("Ufida.T.EAP.Voucher.WebComponent.", ""); string[] arr = strRelativePath.Split('.'); string strCurrentFileDirectory = String.Empty; string strFileName = String.Empty; string strTempPath = strOutputDirectory; for (int i = 0; i < arr.Length - 2; i++) { Directory.CreateDirectory(Path.Combine(strTempPath, arr[i])); strTempPath = Path.Combine(strTempPath, arr[i]); } strFileName = arr[arr.Length - 2] + "." + arr[arr.Length - 1]; FileStream fs = new FileStream(Path.Combine(strTempPath, strFileName), FileMode.Create, FileAccess.Write); fs.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); } } } }

NET资源文件生成器(ResourceWorkshop)是专业的资源文件开发工具,集成了VS.NET中的所有资源文件开发工具的功能,是.NET程序员的必备工具。它有中英文两种语言版本,支持多文档窗口,具有独特的文件关联功能,使您编缉资源文件时更加得心应手。 主要功能: 1、支持.NET框架下的三种资源文件格式(.txt文件、.resX文件和.Resources文件)。 2、支持字符串、颜色、图片、光标等各种资源。 3、提供了添加、删除、重命名、剪切、复制、粘贴等常用编缉功能。 4、支持多文档窗口,可同时编缉多个文件。 5、可将不同资源文件格式之间的相互转换。 6、可将多个不同格式的资源文件合并为一个资源文件。 7、提供了资源文件关联功能,便于对照比较多个资源文件。 设置资源文件关联,是本程序的一个特色功能。将多个资源文件关联在一起,并将其中一个作为主动文件,当主动文件的当前资源项改变时,其它文件中的当前资源项也随之变化,指向与之同名的资源。类似Foxbase中数据库之间的关联功能,被动数据库中的记录指针随着主动数据库的记录指针变化。 当被动资源文件中找不到与主动资源文件中同名的资源时,根据不同的设置,可定位到第一项资源或询问是否复制资源或自动复制资源。 请到我们的主页http://www.guohuasoft.com下载最新版本。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值