static DbCmd() { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler); } static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) { string strFielName = args.Name.Split(',')[0]; if (strFielName.ToLower().EndsWith(".resources")) //不處理資源文件 return null; ExtMethod.WriteLog("DbCmd 程序集獲取失敗,準備動態加載:" + args.Name); string parentRoot = AppDomain.CurrentDomain.BaseDirectory; string dllName = GetDllOrExeName(parentRoot, strFielName); if (dllName == "") { parentRoot = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\')); dllName = GetDllOrExeName(parentRoot, strFielName); } if (dllName != null) { ExtMethod.WriteLog("DbCmd 程序集開始加載文件:" + dllName); return Assembly.LoadFile(dllName); } else { ExtMethod.WriteLog("DbCmd 程序集獲取失敗:找不到文件" + strFielName); } return null; } /// <summary> /// 獲取指定名稱的dll路徑 /// </summary> /// <param name="_DirectoryName">目錄路徑</param> /// <param name="_assemablName">程序集名稱</param> /// <returns>返回路徑名稱</returns> static string GetDllOrExeName(string _DirectoryName, string _assemablName) { string filter = _assemablName + ".???"; var subFiles = System.IO.Directory.GetFiles(_DirectoryName, filter, System.IO.SearchOption.AllDirectories); string dllName = ""; foreach (var item in subFiles) //搜索下級目錄 { if (item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { dllName = item; break; } if (item.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { dllName = item; break; } } return dllName; }