读取DLL中的嵌入式资源文件, 以便以静态资源文件的形式布署到站点中.
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();
}
}
}
}