Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 2.77 KB

File metadata and controls

83 lines (67 loc) · 2.77 KB
title GetFileHash Task
description Learn to use the MSBuild GetFileHash task to compute checksums of the contents of a file or set of files.
ms.date 09/15/2023
ms.topic reference
dev_langs
VB
CSharp
C++
helpviewer_keywords
GetFileHash task [MSBuild]
MSBuild, GetFileHash task
author ghogen
ms.author ghogen
ms.subservice msbuild

GetFileHash task

Computes checksums of the contents of a file or set of files.

This task was added in 15.8, but requires a workaround to use for MSBuild versions below 16.0.

Task parameters

The following table describes the parameters of the GetFileHash task.

Parameter Description
Files Required xref:Microsoft.Build.Framework.ITaskItem[] parameter.

The files to be hashed.
Items xref:Microsoft.Build.Framework.ITaskItem[] output parameter.

The Files input with additional metadata set to the file hash.
Hash String output parameter.

The hash of the file. This output is only set if there was exactly one item passed in.
Algorithm Optional String parameter.

The algorithm. Allowed values: SHA256, SHA384, SHA512. Default = SHA256.
MetadataName Optional String parameter.

The metadata name where the hash is stored in each item. Defaults to FileHash.
HashEncoding Optional String parameter.

The encoding to use for generated hashes. Defaults to hex. Allowed values = hex, base64.

Example

The following example uses the GetFileHash task to determine and print the checksum of the FilesToHash items.

<Project>
  <ItemGroup>
    <FilesToHash Include="$(MSBuildThisFileDirectory)\*" />
  </ItemGroup>
  <Target Name="GetHash">
    <GetFileHash Files="@(FilesToHash)">
      <Output
          TaskParameter="Items"
          ItemName="FilesWithHashes" />
    </GetFileHash>

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

With a single file, you can use the Hash output parameter. The following example project is named hash-example.proj and computes a hash for itself:

<Project>
    <ItemGroup>
      <FileToHash Include="$(MSBuildThisFileDirectory)hash-example.proj" />
    </ItemGroup>
    <Target Name="GetHash">
      <GetFileHash Files="@(FileToHash)">
        <Output
            TaskParameter="Hash"
            ItemName="FileHash" />
      </GetFileHash>
  
      <Message Importance="High"
               Text="File: @(FileToHash) Hash: @(FileHash)" />
    </Target>
  </Project>

See also