문제

대형 프로그램의 작은 기능 중 하나는 폴더의 어셈블리를 검사하고 오래된 어셈블리를 최신 버전으로 대체합니다. 이를 달성하려면 실제로 어셈블리를 실행 프로세스에로드하지 않고 기존 어셈블리 파일의 버전 번호를 읽어야합니다.

도움이 되었습니까?

해결책

나는 다음을 발견했다 이 기사에서.

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

다른 팁

파일에 따라 하나의 옵션이있을 수 있습니다 FileVersionInfo - 즉

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

문제는 이것이 코드에 달려 있다는 것입니다. [AssemblyFileVersion] 속성, 그리고 일치합니다 [AssemblyVersion] 기인하다.

그래도 다른 사람들이 제안한 어셈블리 옵션을 먼저 살펴볼 것이라고 생각합니다.

사용 AssemblyName.GetAssemblyName("assembly.dll");, 그런 다음 이름을 구문 분석하십시오. 에 따르면 MSDN:

파일에 어셈블리가 포함 된 경우에만 작동합니다. 이 방법으로 인해 파일이 열리고 닫히지 만 어셈블리는이 도메인에 추가되지 않습니다.

레코드를 위해 : C#.NET Compact Framework에서 파일 버전을 얻는 방법은 다음과 같습니다. 기본적으로 OpenNETCF에서 비롯되지만 상당히 짧고 삭감되어 Copy'n'Pasted가 할 수 있습니다. 도움이되기를 바랍니다 ...

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top