アセンブリのバージョンをロードせずに取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/187999

  •  06-07-2019
  •  | 
  •  

質問

大規模なプログラムの1つの小さな関数は、フォルダー内のアセンブリを調べ、古いアセンブリを最新バージョンに置き換えます。これを実現するには、実行中のプロセスに実際にそれらのアセンブリをロードすることなく、既存のアセンブリファイルのバージョン番号を読み取る必要があります。

役に立ちましたか?

解決

次のこの記事内

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

他のヒント

ファイルに応じて、1つのオプションは FileVersionInfo です。つまり、

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

問題は、これが [AssemblyFileVersion] 属性を持つコードに依存し、 [AssemblyVersion] 属性と一致することです。

ただし、他の人が最初に提案したAssemblyNameオプションを最初に見ると思います。

AssemblyName.GetAssemblyName(&quot; assembly.dll&quot;); を使用して、名前を解析します。 MSDN によると:

  

これは、ファイルが   アセンブリマニフェストが含まれています。この   メソッドによりファイルが開かれます   閉じますが、アセンブリは   このドメインに追加されました。

記録のみ:C#.NET Compact Frameworkでファイルバージョンを取得する方法は次のとおりです。それは基本的にOpenNETCFからのものですが、かなり短くて正確なので、コピーアンドペーストすることができます。それが役立つことを願っています...

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