如何将System.Data.SQLite合并到单个可执行程序中?

时间:2011-07-07 19:31:31

标签: c# sqlite

我正在尝试在C#中创建一个单可执行应用程序,其中包括SQLite。 System.Data.SQLite依赖于一个非托管DLL(SQLite.Interop.dll),因此我无法将其与ILMerge合并。

如何将System.Data.SQLite捆绑到我的项目中,这样我就可以生成一个没有tag-along DLL的单可执行应用程序?

1 个答案:

答案 0 :(得分:4)

您可以将dll作为嵌入式资源包含在可执行文件中,然后在运行时将其解压缩(这假定程序有权写入您正在提取dll的任何目录)。

这样的东西
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();
}