Get the App
SLTechnology News&Howtos  ›  Network Security  › 

C # get files MD5 and SHA1

Shulou Source: shulou.com Published: 2022-06-01 03:36:42 11月25日 Update

The first method directly uses the FileInfo class constructor to pass in the path, because it is convenient for the console to drag and drop files directly.

It is important to note that there are spaces in the path that will report an error.

In this method, s represents the incoming file path.

1 static void GetFile (string s) 2 {3 try 4 {5 FileInfo fi = new FileInfo (s); 6 Console.WriteLine ("File path: {0}", s); 7 Console.WriteLine ("File name: {0}", fi.Name.ToString ()) 8 Console.WriteLine ("File Type: {0}", fi.Extension.ToString ()); 9 Console.WriteLine ("File size: {0} K", fi.Length / 1024); 10 Console.WriteLine ("File creation time: {0}", fi.CreationTime.ToString ()) 11 Console.WriteLine ("Last access time: {0}", fi.LastAccessTime.ToString ()); 12 Console.WriteLine ("Last write time: {0}", fi.LastWriteTime.ToString ()); 13} 14 catch (Exception ex) 15 {16 Console.WriteLine (ex.Message); 17} 18}

The second method gets the MD5 value, where s represents the incoming file path

MD5 treats the whole file as a large text message and produces this unique MD5 message summary through its irreversible string transformation algorithm.

1 static void GetMD5 (string s) 2 {3 try 4 {5 FileStream file = new FileStream; 6 MD5 md5 = new MD5CryptoServiceProvider (); 7 byte [] retval = md5.ComputeHash (file); 8 file.Close (); 9 10 StringBuilder sc = new StringBuilder () 11 for (int I = 0; I

Tags: File method message path summary representative information time algorithm different same security three two function name size character string console Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Docker MariaDB MySQL