///使用法方:
///一、添加dll文件到资源里(项目-》右键-》属性-》资源-》文件-》添加资源-》添加dll)
///二、添加后项目里会自动生成Resources文件夹,选择你添加的dll文件,在属性里把生成操作改成“嵌入的资源”
///三、在引用里右键-》添加引用-》找到你添加到Resources文件夹里的dll文件,并在属性里,复制本地,设置为false。///
///四、在Program.cs的Main函数最前面加入:blqw.LoadResourceDll.RegistDLL();
///来源: [url=http://www.cnblogs.com/blqw/p/LoadResourceDll.html]http://www.cnblogs.com/blqw/p/LoadResourceDll.html[/url]
///使用流程:ie81.com添加
///
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
namespace blqw
{
/// <summary> 载入资源中的动态链接库(dll)文件
/// </summary>
public static class LoadResourceDll
{
/// <summary> 已加载DLL
/// </summary>
private static Dictionary<string, Assembly> LoadedDlls = new Dictionary<string, Assembly>();
/// <summary> 已处理程序集
/// </summary>
private static Dictionary<string, object> Assemblies = new Dictionary<string, object>();
/// <summary> 在对程序集解释失败时触发
/// </summary>
/// <param name="sender">AppDomain</param>
/// <param name="args">事件参数</param>
private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
//程序集
Assembly ass;
//获取加载失败的程序集的全名
var assName = new AssemblyName(args.Name).FullName;
//判断Dlls集合中是否有已加载的同名程序集
if (LoadedDlls.TryGetValue(assName, out ass) && ass != null)
{
LoadedDlls[assName] = null;//如果有则置空并返回
return ass;
}
else
{
throw new DllNotFoundException(assName);//否则抛出加载失败的异常
}
}
/// <summary> 注册资源中的dll
/// </summary>
/// <param name="pattern">*表示连续的未知字符,_表示单个未知字符,如*.dll</param>
public static void RegistDLL(string pattern = "*.dll")
{
System.IO.Directory.GetFiles("", "");
//获取调用者的程序集
var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
//判断程序集是否已经处理
if (Assemblies.ContainsKey(ass.FullName))
{
return;
}
//程序集加入已处理集合
Assemblies.Add(ass.FullName, null);
//绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
//获取所有资源文件文件名
var res = ass.GetManifestResourceNames();
var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
foreach (var r in res)
{
//如果是dll,则加载
if (regex.IsMatch(r))
{
try
{
var s = ass.GetManifestResourceStream(r);
var bts = new byte[s.Length];
s.Read(bts, 0, (int)s.Length);
var da = Assembly.Load(bts);
//判断是否已经加载
if (LoadedDlls.ContainsKey(da.FullName))
{
continue;
}
LoadedDlls[da.FullName] = da;
}
catch
{
//加载失败就算了...
}
}
}
}
}
}
///一、添加dll文件到资源里(项目-》右键-》属性-》资源-》文件-》添加资源-》添加dll)
///二、添加后项目里会自动生成Resources文件夹,选择你添加的dll文件,在属性里把生成操作改成“嵌入的资源”
///三、在引用里右键-》添加引用-》找到你添加到Resources文件夹里的dll文件,并在属性里,复制本地,设置为false。///
///四、在Program.cs的Main函数最前面加入:blqw.LoadResourceDll.RegistDLL();
///来源: [url=http://www.cnblogs.com/blqw/p/LoadResourceDll.html]http://www.cnblogs.com/blqw/p/LoadResourceDll.html[/url]
///使用流程:ie81.com添加
///
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
namespace blqw
{
/// <summary> 载入资源中的动态链接库(dll)文件
/// </summary>
public static class LoadResourceDll
{
/// <summary> 已加载DLL
/// </summary>
private static Dictionary<string, Assembly> LoadedDlls = new Dictionary<string, Assembly>();
/// <summary> 已处理程序集
/// </summary>
private static Dictionary<string, object> Assemblies = new Dictionary<string, object>();
/// <summary> 在对程序集解释失败时触发
/// </summary>
/// <param name="sender">AppDomain</param>
/// <param name="args">事件参数</param>
private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
//程序集
Assembly ass;
//获取加载失败的程序集的全名
var assName = new AssemblyName(args.Name).FullName;
//判断Dlls集合中是否有已加载的同名程序集
if (LoadedDlls.TryGetValue(assName, out ass) && ass != null)
{
LoadedDlls[assName] = null;//如果有则置空并返回
return ass;
}
else
{
throw new DllNotFoundException(assName);//否则抛出加载失败的异常
}
}
/// <summary> 注册资源中的dll
/// </summary>
/// <param name="pattern">*表示连续的未知字符,_表示单个未知字符,如*.dll</param>
public static void RegistDLL(string pattern = "*.dll")
{
System.IO.Directory.GetFiles("", "");
//获取调用者的程序集
var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
//判断程序集是否已经处理
if (Assemblies.ContainsKey(ass.FullName))
{
return;
}
//程序集加入已处理集合
Assemblies.Add(ass.FullName, null);
//绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
//获取所有资源文件文件名
var res = ass.GetManifestResourceNames();
var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
foreach (var r in res)
{
//如果是dll,则加载
if (regex.IsMatch(r))
{
try
{
var s = ass.GetManifestResourceStream(r);
var bts = new byte[s.Length];
s.Read(bts, 0, (int)s.Length);
var da = Assembly.Load(bts);
//判断是否已经加载
if (LoadedDlls.ContainsKey(da.FullName))
{
continue;
}
LoadedDlls[da.FullName] = da;
}
catch
{
//加载失败就算了...
}
}
}
}
}
}