共用方式為


VerifyFileHash 工作

驗證檔案是否符合預期的檔案雜湊。 如果雜湊不相符,工作就會失敗。

這個工作已在 15.8 中新增,但是需要因應措施以用於 16.0 以下的 MSBuild 版本。

工作參數

下表說明 VerifyFileHash 工作的參數。

參數 描述
File 必要的 String 參數。

要雜湊處理及驗證的檔案。
Hash 必要的 String 參數。

檔案的預期雜湊。
Algorithm 選擇性的 String 參數。

演算法。 允許值:SHA256SHA384SHA512。 預設值 = SHA256
HashEncoding 選擇性的 String 參數。

要用於產生雜湊的編碼。 預設為 hex。 允許值:hexbase64

範例

下列範例會使用 VerifyFileHash 工作來驗證它自己的總和檢查碼。

<Project>
  <Target Name="VerifyHash">
    <GetFileHash Files="$(MSBuildProjectFullPath)">
      <Output
          TaskParameter="Items"
          ItemName="FilesWithHashes" />
    </GetFileHash>

    <Message Importance="High"
             Text="@(FilesWithHashes->'%(Identity): %(FileHash)')" />

    <VerifyFileHash File="$(MSBuildThisFileFullPath)"
                    Hash="$(ExpectedHash)" />
  </Target>
</Project>

在 MSBuild 16.5 和更新版本上,如果您不希望建置在雜湊不相符時失敗,例如,如果您使用雜湊比較做為控制流程的條件,您可以使用下列程式碼將警告降級為訊息:

  <PropertyGroup>
    <MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);MSB3952</MSBuildWarningsAsMessages>
  </PropertyGroup>

  <Target Name="DemoVerifyCheck">
    <VerifyFileHash File="$(MSBuildThisFileFullPath)"
                    Hash="1"
                    ContinueOnError="WarnAndContinue" />

    <PropertyGroup>
      <HashMatched>$(MSBuildLastTaskResult)</HashMatched>
    </PropertyGroup>

    <Message Condition=" '$(HashMatched)' != 'true'"
             Text="The hash didn't match" />

    <Message Condition=" '$(HashMatched)' == 'true'"
             Text="The hash did match" />
  </Target>

另請參閱