Domanda

Una piccola funzione di un programma di grandi dimensioni esamina gli assembly in una cartella e sostituisce gli assembly non aggiornati con le ultime versioni. A tale scopo, è necessario leggere i numeri di versione dei file di assembly esistenti senza caricare tali assembly nel processo di esecuzione.

È stato utile?

Soluzione

Ho trovato il seguente in questo articolo .

using System.Reflection;
using System.IO;

...

// Get current and updated assemblies
AssemblyName currentAssemblyName = AssemblyName.GetAssemblyName(currentAssemblyPath);
AssemblyName updatedAssemblyName = AssemblyName.GetAssemblyName(updatedAssemblyPath);

// Compare both versions
if (updatedAssemblyName.Version.CompareTo(currentAssemblyName.Version) <= 0)
{
    // There's nothing to update
    return;
}

// Update older version
File.Copy(updatedAssemblyPath, currentAssemblyPath, true);

Altri suggerimenti

A seconda dei file, un'opzione potrebbe essere FileVersionInfo - ovvero

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path)
string ver = fvi.FileVersion;

Il problema è che questo dipende dal codice che ha l'attributo [AssemblyFileVersion] e che corrisponde all'attributo [AssemblyVersion] .

Penso che esaminerei prima le opzioni AssemblyName suggerite da altri.

Usa AssemblyName.GetAssemblyName (" assembly.dll "); , quindi analizza il nome. Secondo MSDN :

  

Funzionerà solo se il file   contiene un manifest di assembly. Questo   Il metodo provoca l'apertura del file   e chiuso, ma l'assemblea no   aggiunto a questo dominio.

Solo per la cronaca: ecco come ottenere la versione del file in C # .NET Compact Framework. È fondamentalmente da OpenNETCF, ma piuttosto breve ed estratto, quindi può essere copiato. Spero che ti sia d'aiuto ...

public static Version GetFileVersionCe(string fileName)
{
    int handle = 0;
    int length = GetFileVersionInfoSize(fileName, ref handle);
    Version v = null;
    if (length > 0)
    {
        IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(length);
        if (GetFileVersionInfo(fileName, handle, length, buffer))
        {
            IntPtr fixedbuffer = IntPtr.Zero;
            int fixedlen = 0;
            if (VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
            {
                byte[] fixedversioninfo = new byte[fixedlen];
                System.Runtime.InteropServices.Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen);
                v = new Version(
                    BitConverter.ToInt16(fixedversioninfo, 10), 
                    BitConverter.ToInt16(fixedversioninfo,  8), 
                    BitConverter.ToInt16(fixedversioninfo, 14),
                    BitConverter.ToInt16(fixedversioninfo, 12));
            }
        }
        Marshal.FreeHGlobal(buffer);
    }
    return v;
}

[DllImport("coredll", EntryPoint = "GetFileVersionInfo", SetLastError = true)]
private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);
[DllImport("coredll", EntryPoint = "GetFileVersionInfoSize", SetLastError = true)]
private static extern int GetFileVersionInfoSize(string filename, ref int handle);
[DllImport("coredll", EntryPoint = "VerQueryValue", SetLastError = true)]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top