private void ButtonOK_Click(object sender, RoutedEventArgs e) { if (!TextBox.Text.Equals("Введите имя...") && !TextBox.Text.Equals(string.Empty)) { player.Name = TextBox.Text; bool addRecords = true; bool rewrite = false; foreach (Record item in records) { if (string.Compare(item.Name, player.Name, true) == 0) { if (item.Score < player.Score) { item.Score = player.Score; rewrite = true; } addRecords = false; break; } } if (addRecords) { records.Add(player); rewrite = true; } if (rewrite) { try { if (File.Exists("records.bin")) { FileAttributes fileAttributes = File.GetAttributes("records.bin"); if (fileAttributes.HasFlag(FileAttributes.ReadOnly)) { File.SetAttributes("records.bin", fileAttributes & ~FileAttributes.ReadOnly); } } byte[] bytes = AesCrypt.EncryptStringToBytes(string.Join <Record>("%", records.ToArray()), Encoding.ASCII.GetBytes("zxcvqwerasdfqazx"), Encoding.ASCII.GetBytes("qazxcvbnmlpoiuyt")); using (FileStream fileStream = File.Create("records.bin")) { fileStream.Write(bytes, 0, bytes.Length); } File.SetAttributes("records.bin", File.GetAttributes("records.bin") | FileAttributes.ReadOnly); } catch (Exception exception) { Hide(); MainWindow.ShowError(exception.Message); } } Close(); } }
static void Main(string[] args) { List <Level> level1 = new List <Level>(); List <Level> level2 = new List <Level>(); List <Level> level3 = new List <Level>(); List <Record> records = new List <Record>(); SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); try { connection.Open(); SqlCommand cmd = connection.CreateCommand(); ReadData(cmd, "level1", level1); ReadData(cmd, "level2", level2); ReadData(cmd, "level3", level3); ReadData(cmd, records); connection.Close(); try { DelAttributes("data.bin"); using (FileStream fileStream = File.Create("data.bin")) { string[] temp = new string[3]; temp[0] = string.Join <Level>("%", level1.ToArray()); temp[1] = string.Join <Level>("%", level2.ToArray()); temp[2] = string.Join <Level>("%", level3.ToArray()); byte[] bytes = AesCrypt.EncryptStringToBytes(string.Join(";", temp), Encoding.ASCII.GetBytes("zxcvqwerasdfqazx"), Encoding.ASCII.GetBytes("qazxcvbnmlpoiuyt")); fileStream.Write(bytes, 0, bytes.Length); } SetAttributes("data.bin"); DelAttributes("records.bin"); using (FileStream fileStream = File.Create("records.bin")) { byte[] bytes = AesCrypt.EncryptStringToBytes(string.Join <Record>("%", records.ToArray()), Encoding.ASCII.GetBytes("zxcvqwerasdfqazx"), Encoding.ASCII.GetBytes("qazxcvbnmlpoiuyt")); fileStream.Write(bytes, 0, bytes.Length); } SetAttributes("records.bin"); byte[] fileData; using (FileStream fileStream = File.OpenRead("data.bin")) { fileData = new byte[fileStream.Length]; fileStream.Read(fileData, 0, (int)fileStream.Length); } byte[] checkSum; using (MD5 md5 = new MD5CryptoServiceProvider()) { checkSum = md5.ComputeHash(fileData); } DelAttributes("data.md5"); using (FileStream fileStream = File.Create("data.md5")) { fileStream.Write(checkSum, 0, checkSum.Length); } SetAttributes("data.md5"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } catch (SqlException ex) { if (connection.State != System.Data.ConnectionState.Closed) { connection.Close(); } Console.WriteLine(ex.Message); Console.ReadKey(); } }