Liath
Liath

Reputation: 10191

Check the version of an assembly without loading it

I have a method which checks the version of an assembly and if it's out of date overrides it. To detect the version I'm using the following code:

version = Type.GetType(typeName).Assembly.GetName().Version;

However, if I deem that this version is out of date when I try and override it I get the following error

The process cannot access the file... because it is being used by another process.

It seems to me that the act of reflecting the assembly is causing it to load into the program and so it can't be overridden.

How can I either

  1. Check the version without loading the assembly
  2. Unload the assembly before overriding it

Upvotes: 3

Views: 1366

Answers (3)

sehe
sehe

Reputation: 392833

Consider using Mono.Cecil

which is a superior (well, pre-Roslyn) CIL assembly reader/writer.

Upvotes: 0

Mario Stopfer
Mario Stopfer

Reputation: 551

Load the copy of your assembly in the following way.

var assembly = Assembly.Load(File.ReadAllBytes("YourAssembly.dll"));

You can now override the assembly that's on your disk.

Upvotes: 2

jason
jason

Reputation: 241583

You should just redirect assembly versions.

Upvotes: 2

Related Questions