static string TryExtractDLL(string dirName, string name, string bitness)
{
string fileName = name + ".dll";
try
{
if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
}
catch (Exception)
{
return null;
}
string dllPath = Path.Combine(dirName, fileName);
string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
var namespaceName = typeof(NativeLoader).Namespace;
var resourceName = namespaceName + ".Properties.scaliendb_client." + bitness + ".dll";
// Get the embedded resource stream that holds the Internal DLL in this assembly.
// The name looks funny because it must be the default namespace of this project
// (MyAssembly.) plus the name of the Properties subdirectory where the
// embedded resource resides (Properties.) plus the name of the file.
using (Stream stm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
// Check if the file exists and it is the correct version
if (File.Exists(dllPath))
{
FileInfo fi = new FileInfo(dllPath);
if (fi.Length == stm.Length)
{
// TODO: calculate checksum
return dllPath;
}
}
// Copy the assembly to the temporary file
try
{
using (Stream outFile = File.Create(dllPath))
{
const int sz = 1024 * 1024;
byte[] buf = new byte[sz];
while (true)
{
int nRead = stm.Read(buf, 0, sz);
if (nRead < 1)
break;
outFile.Write(buf, 0, nRead);
}
}
}
catch
{
// This may happen if another process has already created and loaded the file.
// Since the directory includes the version number of this assembly we can
// assume that it's the same bits, so we just ignore the excecption here and
// load the DLL.
}
}
return dllPath;
}