首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

错误来自 HRESULT:0x80131040 System.IO.FileLoadException

2013-11-26 
异常来自 HRESULT:0x80131040System.IO.FileLoadException:本帖最后由 mpy2003 于 2013-11-24 23:45:42 编

异常来自 HRESULT:0x80131040 System.IO.FileLoadException:
本帖最后由 mpy2003 于 2013-11-24 23:45:42 编辑

************** 异常文本 **************
System.IO.FileLoadException: 未能加载文件或程序集“Windows”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
文件名:“Windows”
   在 System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   在 System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   在 System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   在 System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   在 System.Reflection.Assembly.Load(String assemblyString)
   在 WindowsApp.MDIBOX.CreateForm(String strName, String namespaceName, Form MdiParentForm)
   在 WindowsApp.MDIBOX.TOOLS_AfterSelect(Object sender, TreeViewEventArgs e)
   在 System.Windows.Forms.TreeView.OnAfterSelect(TreeViewEventArgs e)
   在 System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
   在 System.Windows.Forms.TreeView.WmNotify(Message& m)
   在 System.Windows.Forms.TreeView.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
正常情况下不会有这个问题,出现这个问题是因为更改了程序文件名。求解决方案
[解决办法]
看了,代码修改起来不算太难:

public static void CreateForm(string filePath,  string strName, string namespaceName, Form MdiParentForm)
        {
            int Index = strName.LastIndexOf(".");
            string formName = strName.Substring(Index + 1);
            if (!ShowChildForm(formName, MdiParentForm))
            {
                string path = namespaceName;
                string name = strName;
                Assembly assembly = Assembly.LoadFrom(filePath);
                Form fr = (Form)assembly.CreateInstance(name);
                fr.MdiParent = MdiParentForm;
                fr.Width = 1040;
                fr.Height = 755;
                fr.FormBorderStyle = FormBorderStyle.None;
                fr.StartPosition = FormStartPosition.Manual;
                fr.Show();
            }
        }

调用的时候

                if (this.TOOLS.SelectedNode.Name == "Work")
                {
                    CreateForm(Application.ExecutablePath,"WindowsApp" + "." + Program.userGroup, "WindowsApp", this);
                }


                else
                {
                    CreateForm(Application.ExecutablePath,"WindowsApp" + "." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);
                }


原理是,你虽然改了exe文件的名字,但是assembly内部的自描述的manifest没有修改,manifest描述了assembly name等,Load是使用的assembly name,依旧是WindowsApp,修改成使用LoadFrom,从文件路径载入assembly。命名空间也依旧是WindowsApp,不随着你修改的文件名而改动。

热点排行