コード例 #1
0
        /// <summary>
        /// 计算文件Hash
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private async Task <FileHashInfo> ComputeFileHash(FileInfo file)
        {
            using (SHA256 sha256 = SHA256.Create())
            {
                try
                {
                    using FileStream fileStream = file.Open(FileMode.Open);
                    fileStream.Position         = 0;
                    byte[] hv = await sha256.ComputeHashAsync(fileStream);

                    FileHashInfo fi = new FileHashInfo(file, hv);
                    fileStream.Close();
                    FileHashComputed?.Invoke(this, new FileHashInfoEventArgs(fi));
                    return(fi);
                }
                catch (Exception ex)
                {
                    FileHashError?.Invoke(this, new FileHashErrorEventArgs(new FileHashError(file, ex)));
                    return(new FileHashInfo(file, null));
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="fileHashInfo"></param>
 public FileHashInfoEventArgs(FileHashInfo fileHashInfo)
 {
     FileHash = fileHashInfo;
 }