public static T Deserialize <T>(string FilePath, bool IsEncrypted = false) { if (File.Exists(FilePath)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream file = new FileStream(FilePath, FileMode.Open); byte[] encrypted = (byte[])formatter.Deserialize(file); file.Close(); byte[] desencrypted = (IsEncrypted) ? EncryptionUtility.DecryptByteArray(encrypted) : encrypted; MemoryStream DeserializationStream = new MemoryStream(desencrypted); T data = (T)formatter.Deserialize(DeserializationStream); DeserializationStream.Dispose(); DeserializationStream.Close(); return(data); } else { return(default(T)); } }