从内存执行WPF程序集

时间:2011-05-01 15:25:48

标签: wpf reflection .net-assembly

如下所示,此方法here适用于大多数应用程序。

FileStream fs = new FileStream(filePath, FileMode.Open); // read the bytes from the application EXE file
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin); // load the bytes into Assembly
MethodInfo method = a.EntryPoint; // search for the Entry Point
if (method != null) 
{
     // create an instance of the Startup form Main method
     object o = a.CreateInstance(method.Name);
     // invoke the application starting point
     method.Invoke(o, null); //EXCEPTION THROWN HERE
}

然而,当我尝试使用它来启动WPF应用程序时,抛出了一个异常:

Exception has been thrown by the target of an invocation.

内部异常的类型为System.IO.IOException:

Cannot locate resource 'mainwindow.xaml'.

注意:应用程序通常可以正常运行。为了测试的目的,编译了一个空的wpf应用程序。

1 个答案:

答案 0 :(得分:0)

当您将启动xaml文件移动到其他位置或重命名但忘记更新App.xaml文件时,通常会出现此错误。如果你重命名它只是像这样更新你的App.xaml文件 - StartupUri="mainwindow.xaml"或者如果你将它移动到不同的子文件夹更新 - StartupUri="FolderName/mainwindow.xaml"

相关问题