我正在尝试在C#中创建一个单可执行应用程序,其中包括SQLite。 System.Data.SQLite依赖于一个非托管DLL(SQLite.Interop.dll),因此我无法将其与ILMerge合并。
如何将System.Data.SQLite捆绑到我的项目中,这样我就可以生成一个没有tag-along DLL的单可执行应用程序?
答案 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();
}