Last active
August 29, 2021 20:30
-
-
Save FuncLun/fff2c63c7d37f4ff3d20f70e00bb241f to your computer and use it in GitHub Desktop.
SQLite depends on native DLLs. In some cases, such as running xUnit tests in net48, those DLLs aren't copied to the execution folder. This hack will copy those files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
namespace SQLitePCL | |
{ | |
public class NativeLibraryHack | |
{ | |
public static bool Hacked { get; private set; } | |
public static bool DoHack() | |
{ | |
if (Hacked) return true; | |
try | |
{ | |
const string runtimeFolderName = "/runtimes"; | |
var destinationPath = typeof(SQLitePCL.raw).Assembly.Location | |
.Replace("\\", "/"); | |
var destinationLength = destinationPath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase); | |
var destinationDirectory = destinationPath.Substring(0, destinationLength) + runtimeFolderName; | |
var sourcePath = new Uri(typeof(SQLitePCL.raw).Assembly.CodeBase) | |
.AbsolutePath; | |
var sourceLength = sourcePath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase); | |
var sourceDirectory = sourcePath.Substring(0, sourceLength) + runtimeFolderName; | |
if (Directory.Exists(sourceDirectory)) | |
CopyFilesRecursively(new DirectoryInfo(sourceDirectory), new DirectoryInfo(destinationDirectory)); | |
} | |
catch (Exception ex) | |
{ | |
//Ignore Exception | |
Debug.WriteLine(ex.Message); | |
return false; | |
} | |
return (Hacked = true); | |
} | |
private static void CopyFilesRecursively( | |
DirectoryInfo source, | |
DirectoryInfo target | |
) | |
{ | |
foreach (var dir in source.GetDirectories()) | |
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name)); | |
foreach (var file in source.GetFiles()) | |
{ | |
try | |
{ | |
var destinationFile = Path.Combine(target.FullName, file.Name); | |
if (!File.Exists(destinationFile)) | |
file.CopyTo(destinationFile); | |
} | |
catch (Exception ex) | |
{ | |
//Ignore Exception | |
Debug.WriteLine(ex.Message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment