FileNotFoundException with Assembly.LoadFile on Windows 7 64

本文关键字:Windows on LoadFile with Assembly FileNotFoundException | 更新日期: 2023-09-27 18:30:47

我正在尝试加载一个混合托管/本机 dll,我编译了 32 位和 64 位。如果我在 Windows 7 32 位上运行我的程序,我可以毫无问题地加载 32 位 dll。如果我加载 64 位 dll,我会得到 BadImageFormatException,这是我所期望的。

我遇到的问题是当我在 Windows 7 64 位下进行相同的测试时:

如果我加载 32 位 dll,我会得到 BadImageFormatException。到目前为止,这还可以。

但是如果我加载 64 位 dll,我会得到 FileNotFoundException。而且这个信息是不真实的,因为我事先检查了这个 dll 的存在!

谁能告诉我,为什么我不能在 Windows 64 7 位下加载我的 64 位 dll?

这是我的示例代码:

    private void Button32_Click(object sender, RoutedEventArgs e)
    {
        string path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "x86", "Native.dll");
        LoadAssembly(path);
    }
    private void Button64_Click(object sender, RoutedEventArgs e)
    {
        string path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "amd64", "Native.dll");
        LoadAssembly(path);
    }
    void LoadAssembly(string path)
    {
        if(File.Exists(path))
        {
            MessageBox.Show("Loading " + path);
            Assembly asm = null;
            try
            {
                asm = Assembly.LoadFile(path);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception!!!'n" + ex);
            }
            MessageBox.Show("Success " + (asm == null ? "no" : "yes"));
        }
    }

FileNotFoundException with Assembly.LoadFile on Windows 7 64

您应该尝试知道无法加载哪个程序集,因为您尝试加载的程序集可能具有找不到的引用。或者存在参考,但用于 32 位,而不是 64 位版本

尝试使用 Fusion 日志来准确了解缺少哪个程序集