Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Assembly Class Locking Issue

Assembly Class Locking Issue

Scheduled Pinned Locked Moved C#
announcementhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel M Edwards
    wrote on last edited by
    #1

    I am using the Assembly.LoadFrom() method in my application to extract some information from some DLLs and I am running into a locking problem. Even after I am done with the file and have set the Assembly to null it keeps the file I loaded locked until the application terminates. The problem comes when I try and update the DLL with a new version automatically via the main application. I am unable to copy the newly downloaded version over because the file still reports as locked by Windows. Does anyone know a way to tell the Assembly object to release it's hold on the file it loaded?

    H 1 Reply Last reply
    0
    • D Daniel M Edwards

      I am using the Assembly.LoadFrom() method in my application to extract some information from some DLLs and I am running into a locking problem. Even after I am done with the file and have set the Assembly to null it keeps the file I loaded locked until the application terminates. The problem comes when I try and update the DLL with a new version automatically via the main application. I am unable to copy the newly downloaded version over because the file still reports as locked by Windows. Does anyone know a way to tell the Assembly object to release it's hold on the file it loaded?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      An assembly cannot be unloaded from an AppDomain. In order to make sure the file doesn't remained locked in use, however, you can read-in the file, grab the raw bytes, and load the buffer:

      byte[] assembly;
      using (FileStream fs = new FileStream(pathToAssembly, FileMode.Open,
      FileAccess.Read, FileShare.Read))
      assembly = new byte[fs.Length];
      fs.Read(assembly, 0, assembly.Length);
      fs.Close();
      }
      AppDomain.CurrentDomain.Load(assembly, null);

      Microsoft MVP, Visual C# My Articles

      D 1 Reply Last reply
      0
      • H Heath Stewart

        An assembly cannot be unloaded from an AppDomain. In order to make sure the file doesn't remained locked in use, however, you can read-in the file, grab the raw bytes, and load the buffer:

        byte[] assembly;
        using (FileStream fs = new FileStream(pathToAssembly, FileMode.Open,
        FileAccess.Read, FileShare.Read))
        assembly = new byte[fs.Length];
        fs.Read(assembly, 0, assembly.Length);
        fs.Close();
        }
        AppDomain.CurrentDomain.Load(assembly, null);

        Microsoft MVP, Visual C# My Articles

        D Offline
        D Offline
        Daniel M Edwards
        wrote on last edited by
        #3

        Thanks, that method works great.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups