Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get assembly file version from an archive without unpacking

I use the following code to get a C# assembly file version for files stored on a harddrive.

var vInfo = FileVersionInfo.GetVersionInfo("assemblyPath").FileVersion;

How could I get an assembly file version for a file stored in an archive without unpacking it? Imagine, you don't have a permission to write to a harddrive. You would probably use some in-memory library for opening the archive and checking what you need to know.

like image 921
Ondrej Janacek Avatar asked Oct 18 '13 12:10

Ondrej Janacek


1 Answers

Sorry but you can't without having a phisical file.

The only way to read the FileVersion is to use FileVersionInfo.GetVersionInfo which accept only a path.

And if you use reflector to see how it read the fileversion then you will see some unsafe native internal methods you cannot use...

private static string GetFileVersionString(IntPtr memPtr, string name)
{
    int num;
    string str = "";
    IntPtr zero = IntPtr.Zero;
    if (UnsafeNativeMethods.VerQueryValue(new HandleRef(null, memPtr), name, ref zero, out num) && (zero != IntPtr.Zero))
    {
        str = Marshal.PtrToStringAuto(zero);
    }
    return str;
}

Maybe you could get it with some DllImport. But this is not in my knowledge.

If you settle for AssemblyVersion you can use DotNetZip library:

Assembly assembly;
using (var data = new MemoryStream())
{
    using (ZipFile zip = ZipFile.Read(LocalCatalogZip))
    {
        zip["assembly.dll"].Extract(data);
    }
    data.Seek(0, SeekOrigin.Begin);

    assembly = Assembly.ReflectionOnlyLoad(data.ToArray());
}
var version = assembly.GetName().Version;

----------------UPDATE-----------------

Last thought: maybe you have permission to write a file in the temp folder: Path.GetTempPath

like image 136
giammin Avatar answered Oct 31 '22 13:10

giammin