簡體   English   中英

如何從已從字節加載的程序集中獲取 FileVersion?

[英]How can I get the FileVersion from an Assembly that has been loaded from bytes?

我有一些程序集存儲在數據庫的 Oracle BLOB 字段中。 我正在加載程序集,創建類的實例等,一切都成功了。 但是,我想訪問加載的程序集的 AssemblyFileVersion,但似乎無法找到如何去做。

我嘗試了很多東西,包括類似下面的代碼:

var assembly = Assembly.Load(plugInBytes);
var version = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;

但是,當從字節加載程序集時, assembly.Location為空,之后沒有任何好處。

只是在正確的方向上尋找推動力。

如果應用了AssemblyFileVersion屬性,您不能只使用:

var version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                      .Cast<AssemblyFileVersionAttribute>()
                      .Select(attr => attr.Version)
                      .FirstOrDefault();
if (version != null)
{
    // Got the version number...
}

你可以試試

 public bool GetVersion(string fileName)
 {
       Assembly asm = null;
       try
       {
               asm = Assembly.LoadFrom(fileName);
        }
        catch (Exception err)
        {
               this._errMsg = err.Message;
               return false;
         }
         if (asm != null)
         {
               this._info = new AssemblyInformation();
               this._info.Name = asm.GetName().Name;
               this._info.Version = asm.GetName().Version.ToString();
              this._info.FullName = asm.GetName().ToString();
         }
         else
         {
               this._errMsg = "Invalid assembly";
               return false;
          } 
          return GetReferenceAssembly(asm);
  }
  public bool GetVersion(Assembly asm)
  {
         if (asm != null)
         {
              this._info = new AssemblyInformation();
              this._info.Name = asm.GetName().Name;
             this._info.Version = asm.GetName().Version.ToString();
             this._info.FullName = asm.GetName().ToString();
         }
         else
          {
             this._errMsg = "Invalid assembly";
             return false;
          }

          return GetReferenceAssembly(asm);
    }

如果需要文件版本,只需獲取相同的字節,保存到臨時文件並獲取文件版本。 匯編版本很可能是相同的並且更容易獲得(參見 Jon 的回復)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM