简体   繁体   中英

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.

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

You can use Nuget package Mono.Cecil

ModuleDefinition.ReadModule(stream)

You'll get a ModuleDefinition, containing all desired assembly info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM