如果您观察到以下图片,我的“RSR.exe”应用程序运行完全正常,因为两个.dll位于同一文件路径上;但是,如果这些.dll不与RSR.exe位于同一文件路径/目录中,则会导致崩溃/'程序已停止工作'。
以下是我试图使用的以下代码,但没有成功。
public static MainForm _mainForm;
static string subPath = @"C:\Users\Public\Documents\RSR";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!Directory.Exists(subPath))
{
Directory.CreateDirectory(subPath);
if (IntPtr.Size == 4)
{
File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\MouseKeyboardActivityMonitor.dll", Properties.Resources.MouseKeyboardActivityMonitor);
File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\WindowsFormsAero.dll", Properties.Resources.WindowsFormsAero);
}
else
{
File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\MouseKeyboardActivityMonitor.dll", Properties.Resources.MouseKeyboardActivityMonitor1);
File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\WindowsFormsAero.dll", Properties.Resources.WindowsFormsAero);
}
}
AssemblyName asm1 = AssemblyName.GetAssemblyName(@"C:\Users\Public\Documents\RSR\MouseKeyboardActivityMonitor.dll");
Assembly.Load(asm1);
AssemblyName asm2 = AssemblyName.GetAssemblyName(@"C:\Users\Public\Documents\RSR\WindowsFormsAero.dll");
Assembly.Load(asm2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (_mainForm = new MainForm())
{
Application.Run(_mainForm);
}
Cursor normalCursor1 = new Cursor(new System.IO.MemoryStream(Properties.Resources.cursor_normal));
SetSystemCursor(normalCursor1.Handle, 32512);
}
以下是2 .dll不在同一目录/路径中时崩溃信息的图片。
任何帮助都将不胜感激。
感谢。
答案 0 :(得分:2)
如果必须将DLL打包为嵌入式资源,则可以通过动态程序集解析来加载它。您的代码也必须运行加载权限。
public static MainForm _mainForm;
{
//Add this in your main initialization
AppDomain.CurrentDomain.AssemblyResolve+=new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("FullNameSpace.MouseKeyboardActivityMonitor"))
{
return Assembly.Load(Properties.Resources.MouseKeyboardActivityMonitor);
}
if (args.Name.Contains("FullNameSpace.MouseKeyboardActivityMonitor"))
{
return Assembly.Load(Properties.Resources.WindowsFormsAero);
}
return null;
}