Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge System.Data.SQLite into a single-executable program?

Tags:

c#

sqlite

I'm trying to create a single-executable application in C#, which includes SQLite. System.Data.SQLite depends on one unmanaged DLL (SQLite.Interop.dll), so I can't merge it with ILMerge.

How can I bundle System.Data.SQLite into my project so I can produce a single-executable application with no tag-along DLLs?

like image 545
spiffytech Avatar asked Oct 10 '22 14:10

spiffytech


1 Answers

You can include the dll as an embedded resource in the executable, then at runtime extract it (this assumes the program has permissions to write to whatever directory you are extracting the dll to).

Something like

string sqllitefile = "sqllite.dll";
Assembly currentAssembly = Assembly.GetExecutingAssembly();

using (FileStream fs = fileInfoOutputFile.OpenWrite())
using (Stream resourceStream =currentAssembly.GetManifestResourceStream(sqllitefile))
{
   const int size = 4096;
   byte[] bytes = new byte[4096];
   int numBytes;
   while ((numBytes = resourceStream.Read(bytes, 0, size)) > 0) 
   {
         fs.Write(bytes, 0, numBytes);
   }
   fs.Flush();
   fs.Close();
   resourceStream.Close();
}
like image 151
Chris B Avatar answered Dec 01 '22 00:12

Chris B