现在的位置: 首页 > 综合 > 正文

读取嵌入到 Dll 文件中的资源文件

2013年09月16日 ⁄ 综合 ⁄ 共 913字 ⁄ 字号 评论关闭

private static Hashtable _asmfiles;

public static byte[] LoadAssemblyFiles(string filename)
  {
   if (filename == null)
   {
    throw new ArgumentNullException("filename");
   }
   if (!filename.StartsWith("/"))
   {
    throw new ArgumentException("must starts with '/'", "filename");
   }
   filename = "/" + filename.Remove(0, 1).Replace('/', '.');
   if (Resx._asmfiles == null)
   {
    Hashtable afs = new Hashtable();
    foreach (string resname in typeof(Resx).Assembly.GetManifestResourceNames())
    {
     if (resname.StartsWith("UploadFileHelper.Uploader.File."))
     {
      byte[] buf;
      string fn = "/" + resname.Remove(0, "UploadFileHelper.Uploader.File.".Length);
      fn = fn.ToLower();
      using (Stream s = typeof(Resx).Assembly.GetManifestResourceStream(resname))
      {
       buf = new byte[s.Length];
       s.Read(buf, 0, buf.Length);
      }
      afs.Add(fn, buf);
     }
    }
    Resx._asmfiles = afs;
   }
   return (byte[]) Resx._asmfiles[filename.ToLower()];
  } 

抱歉!评论已关闭.