對於Asp.Net 2.0中指令碼資源的研究(2)
Author: Truly
在上篇文章中我們主要介紹了.net 2.0 中的資源檔,並以WebForms.js為例做了重點分析,有朋友問我如何擷取所有的資源檔,方法有很多中。
可以使用我們熟悉的Reflector匯出所有資源檔(利用Reflector.FileDisassembler外掛程式),也可以使用下面的方法:
遍曆所有資源:
Attribute[] abc = System.Web.UI.WebResourceAttribute.GetCustomAttributes(Assembly.GetAssembly(typeof(System.Web.UI.Page))) ;
int i = 0;
foreach (Attribute a in abc)
{
if (a.TypeId.ToString() == "System.Web.UI.WebResourceAttribute")
{
i++;
Response.Write(((System.Web.UI.WebResourceAttribute)(a)).WebResource + "\r\n");
}
}
Response.Write("<br>總數量:"+ i);
匯出所有的資源檔:
Assembly assm = Assembly.GetAssembly(typeof(System.Web.UI.Page));
Attribute[] abc = System.Web.UI.WebResourceAttribute.GetCustomAttributes(assm);
Stream stream;
System.IO.FileStream sr;
byte[] bt;
string name = "";
WebResourceAttribute wra;
foreach (Attribute a in abc)
{
if (a.TypeId.ToString() == "System.Web.UI.WebResourceAttribute")
{
wra = (WebResourceAttribute)a;
name = wra.WebResource;
stream = assm.GetManifestResourceStream(name);
if (stream == null)
continue;
bt = new byte[stream.Length];
stream.Read(bt, 0, (int)stream.Length);
sr = new FileStream(@"f:\resource\" + name, FileMode.Create);
sr.Write(bt, 0, bt.Length);
sr.Close();
stream.Close();
Response.Write(a.TypeId.ToString() + "<br />");
}
}