C#在运行时自动引入正确的Dlls

设置程序Dlls的搜索路径

1.导入Native函数

/// <summary>
/// 设置Dlls的搜索路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDllDirectory(string path);

2.在引入包装类中设置相应的Dlls的搜索路径

var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
path = Path.Combine(path, Environment.Is64BitProcess ? "x64" : "x86");
// 或者通过指针变量的大小来判断是x86/x64
//path = Path.Combine(path, IntPtr.Size == 4 ? "x86" : "x64");
SetDllDirectory(path);
posted @ 2019-12-09 11:16  非法关键字  阅读(273)  评论(0编辑  收藏  举报