0

I'm trying to delete assembly after loading, but cannot. Please help me. Thanks.

private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
        try
        {
            AssemblyName assemblyName = new AssemblyName(args.Name);
            string folderBeyConsPath = FindInstallBeyConsFolderPath(FolderBeyConsPathKey);
            if (string.IsNullOrEmpty(folderBeyConsPath) || !Directory.Exists(folderBeyConsPath)) return null;
            string file = Directory.GetFiles(folderBeyConsPath, "*.dll").Where(x => Path.GetFileNameWithoutExtension(x) == assemblyName.Name).FirstOrDefault();
            if (!string.IsNullOrEmpty(file))
            {
                byte[] content = File.ReadAllBytes(file);
                return Assembly.Load(content);
            }
        }
        catch { }
        return null;
    }
NhuTruong
  • 11
  • 5
  • 2
    You cannot unload assembly in .NET, known limitation, you need to Unload full Domain - https://learn.microsoft.com/en-us/dotnet/standard/assembly/load-unload – Nikita Chayka Oct 31 '20 at 17:00
  • There are cases, I did the above and deleted the file. I don't know the reason? – NhuTruong Oct 31 '20 at 17:30
  • 1
    It's not entirely clear what you are having trouble with here. If by "delete" you mean you want to unload the assembly that was loaded, then as noted in the duplicate, an assembly cannot be unloaded piecemeal. You have to unload the whole AppDomain into which the assembly was loaded. If by "delete" you instead mean that you want to delete the _file_ that you used in the call to `File.ReadAllBytes()`, there's nothing in the code you posted that would prevent the file from being deleted and you need to improve the question so that it includes a [mcve] that reproduces the problem. – Peter Duniho Oct 31 '20 at 17:38

0 Answers0