| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using Microsoft.Win32;
- using System;
- using System.IO;
- using System.Media;
- using System.Windows.Forms;
- namespace RegistrySample
- {
- public partial class Form1 : Form
- {
- const string KEY = "Software\\PrgDemo1";
- const string STRING_VALUE = "fam";
- const string INTEGER_VALUE = "age";
- const string BINARY_VALUE = "bin";
- byte[] currentFile = null;
- string currentFilename = "";
- public Form1()
- {
- InitializeComponent();
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- using (RegistryKey key = Registry.CurrentUser.CreateSubKey(KEY))
- {
- key.SetValue(STRING_VALUE, tbxLastname.Text);
- key.SetValue(INTEGER_VALUE, int.Parse(tbxAge.Text));
- key.SetValue(BINARY_VALUE, currentFile);
- MessageBox.Show("Данные записаны");
- }
- }
- private void btnRead_Click(object sender, EventArgs e)
- {
- using (RegistryKey key = Registry.CurrentUser.OpenSubKey(KEY))
- {
- if (key == null)
- {
- MessageBox.Show("Ключ не найден");
- return;
- }
- if (key.GetValue(STRING_VALUE) != null) tbxLastname.Text = (string)key.GetValue(STRING_VALUE);
- else MessageBox.Show("Ошибка чтения фамилии");
- if (key.GetValue(INTEGER_VALUE) != null) tbxAge.Text = key.GetValue(INTEGER_VALUE).ToString();
- else MessageBox.Show("Ошибка чтения возраста");
- if (key.GetValue(BINARY_VALUE) != null) currentFile = (byte[])key.GetValue(BINARY_VALUE);
- else MessageBox.Show("Ошибка чтения аудиофайла");
- if (currentFile == null) MessageBox.Show("Ошибка чтения аудиофайла");
- else
- {
- using (SoundPlayer sp = new SoundPlayer())
- {
- using (MemoryStream ms = new MemoryStream(currentFile))
- {
- sp.Stream = ms;
- sp.Load();
- sp.Play();
- }
- }
- }
- }
- }
- private void btnRemoveValue_Click(object sender, EventArgs e)
- {
- using (RegistryKey key = Registry.CurrentUser.OpenSubKey(KEY, true))
- {
- if (key == null)
- {
- MessageBox.Show("Ключ не найден");
- return;
- }
- if (key.GetValue(STRING_VALUE) != null) key.DeleteValue(STRING_VALUE);
- if (key.GetValue(INTEGER_VALUE) != null) key.DeleteValue(INTEGER_VALUE);
- if (key.GetValue(BINARY_VALUE) != null) key.DeleteValue(BINARY_VALUE);
- currentFilename = "";
- currentFile = null;
- MessageBox.Show("Данные из реестра удалены");
- }
- }
- private void btnRemoveKey_Click(object sender, EventArgs e)
- {
- using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true))
- {
- try
- {
- key.DeleteSubKeyTree("PrgDemo1");
- MessageBox.Show("Ключ удалён");
- }
- catch
- {
- MessageBox.Show("Ключа не существует");
- }
- }
- }
- private void btnBrowseFile_Click(object sender, EventArgs e)
- {
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- //currentFile = File.ReadAllBytes(openFileDialog.FileName);
- // второй вариант чтения массива байт из бинарного файла
- BinaryReader br = null;
- try
- {
- br = new BinaryReader(File.Open(openFileDialog.FileName, FileMode.Open));
- currentFile = br.ReadBytes((int)br.BaseStream.Length);
- }
- catch
- {
- }
- finally
- {
- br.Close();
- }
- lblFilename.Text = "Аудио файл: " + openFileDialog.FileName;
- currentFilename = openFileDialog.FileName;
- }
- else
- {
- lblFilename.Text = "Аудио файл: ";
- currentFile = null;
- }
- }
- }
- }
|