Search code examples
c#live-update

Dynamically un referenc a dll in c# winforms


I have a winform c# app.

I can use a [web method] to check for a version of a DLL that my client winform should use. I can also download it. But, if I copy over the old DLL I will get an error stating that the file is in use. I had expected this.

Is there a way to unload this DLL from my running winform app, copy over the DLL and reload it?

I do not want to use a boot-strapper for my updater and it may come to having to stop and restart my application.

All this will be obsolete if I can just unload my DLL.

I have looked for a long time and cannot find anything. Before I retire on this I thought I would post the question here.

Thanks

PS I could post the code that shows how to load an DLL in code and the error generated from it but it is open knowledge and does not add to this question.


Solution

  • Is there a way to unload this DLL from my running winform app, copy over the DLL and reload it?

    Not in most cases. The only way to unload a DLL in a managed application is to unload the entire AppDomain. This typically means you need to be very careful about how you use the assembly (you can't just directly reference and use it as normal).

    I do not want to use a boot-strapper for my updater and it may come to having to stop and restart my application.

    This is the most common approach. Making this work inside the application is essentially building the bootstrapper into the application itself (in its own AppDomain), and unloading/restarting the AppDomain using the assembly. This is typically more work and just as disrupting as a separate bootstrapper application.