Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - How to detect .NET Framework version of an Assembly

Reply
 
Old 05-12-2008   #1 (permalink)
Hayato Iriumi


 
 

How to detect .NET Framework version of an Assembly

Hello,
I have a need to know which version of the .NET Framework an assembly
was compiled against. Well, I found a way to do it by detecting
Assembly.ImageRuntimeVersion, but once I load the assembly, I cannot
delete the file because the file is held by the upgrade application
after I load the assembly. The reason I want to be able to delete is
that I'm writing an application to upgrade a Windows Service written
in .NET. By using System.Reflection.Assembly.Load(), there isn't a way
to unload the assembly.

So let's say I have a Windows Service executable written against .NET
Framework 1.1 and I need to upgrade it to the Windows Service written
against .NET Framework 2.0. If I already know that the executable was
written against 1.1, this is easy, but that's not always the case. I
need to be able to use the correct version of InstallUtil to properly
uninstall the existing Windows Service, which is why I want to detect
the ImageRuntimeVersion of the assembly. After that, I want to be able
to copy the new files to the same directory and install it.

I tried to load it into a separate AppDomain, but I got an error
saying that "Could not load file or assembly...." Here is the code I
did.


AppDomainSetup info = new AppDomainSetup();
info.ApplicationName = "HelloWorld";
info.ApplicationBase =
Path.GetDirectoryName(ServicePath);
info.PrivateBinPath =
Path.GetDirectoryName(ServicePath);

AppDomain ad = AppDomain.CreateDomain("TempAppDomain",
null, info);
Assembly ass = ad.Load(AssemblyByteArray);

string ClrPath =
XCCommon.GetCLRPath(ass.ImageRuntimeVersion);
AppDomain.Unload(ad);

After all, all I want to be able to do is to detect the version of
the .NET Framework that an assembly was compiled against and be able
to delete the assembly.

My System SpecsSystem Spec
Old 05-12-2008   #2 (permalink)
Hayato Iriumi


 
 

Re: How to detect .NET Framework version of an Assembly

I think I took a wrong direction to solve the problem. After chilling
for a few minutes gave me a different and yet simpler idea.

All I needed was to load an assembly without the upgrade application
holding onto the file. Here is what I did and it seems to work
although I'm not really unloading the assembly from the memory.

byte[] AssemblyByteArray =
System.IO.File.ReadAllBytes(ServicePath);
Assembly ass = Assembly.Load(AssemblyByteArray);
string ClrPath =
XCCommon.GetCLRPath(ass.ImageRuntimeVersion);
ExecutableLauncher.Run(System.IO.Path.Combine(ClrPath,
"InstallUtil.exe"), "/u \"" + ServicePath + "\"");


On May 12, 3:56 pm, Hayato Iriumi <hiri...@xxxxxx> wrote:
Quote:

> Hello,
> I have a need to know which version of the .NET Framework an assembly
> was compiled against. Well, I found a way to do it by detecting
> Assembly.ImageRuntimeVersion, but once I load the assembly, I cannot
> delete the file because the file is held by the upgrade application
> after I load the assembly. The reason I want to be able to delete is
> that I'm writing an application to upgrade a Windows Service written
> in .NET. By using System.Reflection.Assembly.Load(), there isn't a way
> to unload the assembly.
>
> So let's say I have a Windows Service executable written against .NET
> Framework 1.1 and I need to upgrade it to the Windows Service written
> against .NET Framework 2.0. If I already know that the executable was
> written against 1.1, this is easy, but that's not always the case. I
> need to be able to use the correct version of InstallUtil to properly
> uninstall the existing Windows Service, which is why I want to detect
> the ImageRuntimeVersion of the assembly. After that, I want to be able
> to copy the new files to the same directory and install it.
>
> I tried to load it into a separate AppDomain, but I got an error
> saying that "Could not load file or assembly...." Here is the code I
> did.
>
> AppDomainSetup info = new AppDomainSetup();
> info.ApplicationName = "HelloWorld";
> info.ApplicationBase =
> Path.GetDirectoryName(ServicePath);
> info.PrivateBinPath =
> Path.GetDirectoryName(ServicePath);
>
> AppDomain ad = AppDomain.CreateDomain("TempAppDomain",
> null, info);
> Assembly ass = ad.Load(AssemblyByteArray);
>
> string ClrPath =
> XCCommon.GetCLRPath(ass.ImageRuntimeVersion);
> AppDomain.Unload(ad);
>
> After all, all I want to be able to do is to detect the version of
> the .NET Framework that an assembly was compiled against and be able
> to delete the assembly.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Advice on assembly version conflicts .NET General
.NET Framework version PowerShell
.NET Framework version PowerShell
.Net framework version?? Vista performance & maintenance
.NET Framework Version 2.0 7 Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46