Pergunta

Uma função pequena de um grande montagens focalizados o Programa em uma pasta e substitui montagens out-of-date com as últimas versões. Para conseguir isso, ele precisa ler os números de versão dos arquivos de montagem existentes sem realmente carregar esses módulos para o processo de execução.

Foi útil?

Solução

Eu encontrei o seguinte neste artigo .

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);

Outras dicas

Dependendo dos ficheiros, uma opção pode ser FileVersionInfo -. I

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

O problema é que isso depende do código ter o atributo [AssemblyFileVersion], e combinando o atributo [AssemblyVersion].

Eu acho que eu olhar para as opções AssemblyName sugeridos por outros em primeiro lugar, no entanto.

Use AssemblyName.GetAssemblyName("assembly.dll");, em seguida, analisar o nome. De acordo com a MSDN :

Isto só irá funcionar se o arquivo contém um manifesto de montagem. este método faz com que o arquivo a ser aberto e fechado, mas o conjunto não está adicionados a este domínio.

Apenas para o registro: Aqui está como obter a versão do arquivo em C # .NET Compact Framework. É basicamente a partir OpenNETCF mas muito mais curto e exctacted para que ele possa por copy'n'pasted. Espero que ele vai ajudar ...

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top