Form1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Media;
  5. using System.Windows.Forms;
  6. namespace RegistrySample
  7. {
  8. public partial class Form1 : Form
  9. {
  10. const string KEY = "Software\\PrgDemo1";
  11. const string STRING_VALUE = "fam";
  12. const string INTEGER_VALUE = "age";
  13. const string BINARY_VALUE = "bin";
  14. byte[] currentFile = null;
  15. string currentFilename = "";
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. private void btnSave_Click(object sender, EventArgs e)
  21. {
  22. using (RegistryKey key = Registry.CurrentUser.CreateSubKey(KEY))
  23. {
  24. key.SetValue(STRING_VALUE, tbxLastname.Text);
  25. key.SetValue(INTEGER_VALUE, int.Parse(tbxAge.Text));
  26. key.SetValue(BINARY_VALUE, currentFile);
  27. MessageBox.Show("Данные записаны");
  28. }
  29. }
  30. private void btnRead_Click(object sender, EventArgs e)
  31. {
  32. using (RegistryKey key = Registry.CurrentUser.OpenSubKey(KEY))
  33. {
  34. if (key == null)
  35. {
  36. MessageBox.Show("Ключ не найден");
  37. return;
  38. }
  39. if (key.GetValue(STRING_VALUE) != null) tbxLastname.Text = (string)key.GetValue(STRING_VALUE);
  40. else MessageBox.Show("Ошибка чтения фамилии");
  41. if (key.GetValue(INTEGER_VALUE) != null) tbxAge.Text = key.GetValue(INTEGER_VALUE).ToString();
  42. else MessageBox.Show("Ошибка чтения возраста");
  43. if (key.GetValue(BINARY_VALUE) != null) currentFile = (byte[])key.GetValue(BINARY_VALUE);
  44. else MessageBox.Show("Ошибка чтения аудиофайла");
  45. if (currentFile == null) MessageBox.Show("Ошибка чтения аудиофайла");
  46. else
  47. {
  48. using (SoundPlayer sp = new SoundPlayer())
  49. {
  50. using (MemoryStream ms = new MemoryStream(currentFile))
  51. {
  52. sp.Stream = ms;
  53. sp.Load();
  54. sp.Play();
  55. }
  56. }
  57. }
  58. }
  59. }
  60. private void btnRemoveValue_Click(object sender, EventArgs e)
  61. {
  62. using (RegistryKey key = Registry.CurrentUser.OpenSubKey(KEY, true))
  63. {
  64. if (key == null)
  65. {
  66. MessageBox.Show("Ключ не найден");
  67. return;
  68. }
  69. if (key.GetValue(STRING_VALUE) != null) key.DeleteValue(STRING_VALUE);
  70. if (key.GetValue(INTEGER_VALUE) != null) key.DeleteValue(INTEGER_VALUE);
  71. if (key.GetValue(BINARY_VALUE) != null) key.DeleteValue(BINARY_VALUE);
  72. currentFilename = "";
  73. currentFile = null;
  74. MessageBox.Show("Данные из реестра удалены");
  75. }
  76. }
  77. private void btnRemoveKey_Click(object sender, EventArgs e)
  78. {
  79. using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true))
  80. {
  81. try
  82. {
  83. key.DeleteSubKeyTree("PrgDemo1");
  84. MessageBox.Show("Ключ удалён");
  85. }
  86. catch
  87. {
  88. MessageBox.Show("Ключа не существует");
  89. }
  90. }
  91. }
  92. private void btnBrowseFile_Click(object sender, EventArgs e)
  93. {
  94. if (openFileDialog.ShowDialog() == DialogResult.OK)
  95. {
  96. //currentFile = File.ReadAllBytes(openFileDialog.FileName);
  97. // второй вариант чтения массива байт из бинарного файла
  98. BinaryReader br = null;
  99. try
  100. {
  101. br = new BinaryReader(File.Open(openFileDialog.FileName, FileMode.Open));
  102. currentFile = br.ReadBytes((int)br.BaseStream.Length);
  103. }
  104. catch
  105. {
  106. }
  107. finally
  108. {
  109. br.Close();
  110. }
  111. lblFilename.Text = "Аудио файл: " + openFileDialog.FileName;
  112. currentFilename = openFileDialog.FileName;
  113. }
  114. else
  115. {
  116. lblFilename.Text = "Аудио файл: ";
  117. currentFile = null;
  118. }
  119. }
  120. }
  121. }