c# - Windows 7 64 位上带有 Assembly.LoadFile 的 FileNotFoundException

标签 c# .net 64-bit assemblies

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

我遇到的问题是当我在 Windows 7 64 位下做同样的测试时:

如果我加载 32 位 dll,我会得到 BadImageFormatException。到目前为止没问题。

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

谁能告诉我,为什么我不能在 Windows 7 64 位下加载我的 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"));
        }
    }

最佳答案

您应该尝试了解无法加载哪个程序集,因为您尝试加载的程序集可能包含无法找到的引用。或存在引用,但适用于 32 位,而不是 64 位版本

尝试使用Fusion log确切知道缺少哪个程序集

关于c# - Windows 7 64 位上带有 Assembly.LoadFile 的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10085120/

相关文章:

windows - SVN、Apache、Win7 x64 - 在哪里可以找到 mod_dav_svn 和 mod_dav_auth?

c# - 在 C# 中显示文本弹出窗口

c# - 您可以将 Microsoft.AspNet.SignalR.Client 与 .NET Core (dnxcore) 一起使用吗

ios - arc4random_uniform 64 位 ios

c# - 如何使用 LINQ 在组内进行排序

.net - ORA-21700 : object does not exist or is marked for delete for Associative Array as input parameter called from ODP. 网络

Android 应用 64 位支持截止日期推迟到 2020 年?

c# - 按键反序列化JSON到C#对象

c# - 如何为具有 IDictionary<TKey, TValue> 参数的函数制作文档?

c# - 无需完整下载即可读取 zip 文件