Advertisement
ristosr

File Header Modification Class - C#

Apr 29th, 2016
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. class FileUtils
  4. {
  5.     #region VARIABLES
  6.     private const int OFFSET_CHECKSUM = 0x12;
  7.     #endregion
  8.  
  9.     #region METHODS
  10.     public static ushort GetCheckSum(string fileName)
  11.     {
  12.         if (!File.Exists(fileName))
  13.             throw new FileNotFoundException("Invalid fileName");
  14.         return GetCheckSum(File.ReadAllBytes(fileName));
  15.     }
  16.     public static ushort GetCheckSum(byte[] fileData)
  17.     {
  18.         if (fileData.Length < OFFSET_CHECKSUM + 1)
  19.             throw new ArgumentException("Invalid fileData");
  20.         return BitConverter.ToUInt16(fileData, OFFSET_CHECKSUM);
  21.     }
  22.     public static void WriteCheckSum(string sourceFile, string destFile, ushort checkSum)
  23.     {
  24.         if (!File.Exists(sourceFile))
  25.             throw new FileNotFoundException("Invalid fileName");
  26.         WriteCheckSum(File.ReadAllBytes(sourceFile), destFile, checkSum);
  27.     }
  28.     public static void WriteCheckSum(byte[] data, string destFile, ushort checkSum)
  29.     {
  30.         byte[] checkSumData = BitConverter.GetBytes(checkSum);
  31.         checkSumData.CopyTo(data, OFFSET_CHECKSUM);
  32.         File.WriteAllBytes(destFile, data);
  33.     }
  34.     #endregion
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement