Executing WPF Assembly from Memory
This method here, shown below, works well for most applications.
File开发者_如何学GoStream 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
}
However when I tried using it to start a WPF app, an exception was thrown:
Exception has been thrown by the target of an invocation.
The inner exception was of type System.IO.IOException:
Cannot locate resource 'mainwindow.xaml'.
Note: the application runs fine by itself normally. for the purposes of testing, an empty wpf application was compiled.
This error generally comes when you move your startup xaml file into different location or renamed it but forgot to update your App.xaml file. In case you renamed it just update your App.xaml file like this - StartupUri="mainwindow.xaml"
or if you move it to different subfolder update like this - StartupUri="FolderName/mainwindow.xaml"
精彩评论