Calculate MD5 checksum for a file in C#
To calculate MD5 checksum of a file in C# you can create an object of MD5 vy using methodCreate()
of class MD5
and then call ComputeHash()
which accepts a stream as argument and return array of bytes as MD5.Remember to include required namespaces:
using System.Security.Cryptography
, System.IO
Example
using System;
using System.Security.Cryptography;
using System.IO;
public class Client
{
public static void Main()
{
string filename = @"C:\users\user\file.txt";
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
byte[] md5String = md5.ComputeHash(stream);
}
}
}
}