上色

Code Block

2018年6月28日 星期四

[.Net] Embedded dll into exe

1. Add dll to resources, set the build action to "embedded resources" 2. Add following code to "Program.cs"
static void Main()
{
 //load resource dll
 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new frmTDSCreator());
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
 string resourceName = "TDSCreator.Resources." + new AssemblyName(args.Name).Name + ".dll";
 using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
 {
  byte[] assemblyData = new byte[stream.Length];
  stream.Read(assemblyData, 0, assemblyData.Length);
  return Assembly.Load(assemblyData);
 }
}

沒有留言:

張貼留言