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

spi*_*ech 5 c# sqlite

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

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

Chr*_*s B 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();
}
Run Code Online (Sandbox Code Playgroud)

  • @bitbonk - 这将如何与需要引用非托管程序集的第三方托管程序集一起使用(正如问题所讨论的那样)?如果该托管程序集未设置为从内存中读取它,则它将无法使用它。 (2认同)