lucy mafa

lucy mafa

  • NA
  • 1
  • 893

how to save settings to registry

Oct 30 2014 9:52 AM
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
using Microsoft.Win32;
namespace SettingsFile
{
public partial class LogForm : Form
{
public LogForm()
{
InitializeComponent();
var settingsFilePath = GetFileName();
btnOk.Enabled = false;
btnOpenFile.Enabled = true;
}
private void SetControls(IEnumerable<string> settings)
{
foreach (var setting in settings.Where(setting => setting.Contains("=")))
{
switch (setting.Split('=')[0].Trim())
{
case "EnableLogging":
chkLoggin.Checked = Convert.ToBoolean(setting.Split('=')[1].Trim());
break;
case "LogFilePath":
txtFilePath.Text = setting.Split('=')[1].Trim();
break;
case "NumberOfThreads":
txtNumOfThreads.Text = setting.Split('=')[1].Trim();
break;
}
}
}
private void btnOk_Click(object sender, EventArgs e)
{
var settingsFilePath = GetFileName();
foreach (Control control in this.Controls)
{
control.Focus();
if (Validate())
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("EnableLogging = {0}", chkLoggin.Checked).AppendLine();
sb.AppendFormat("LogFilePath = {0}", txtFilePath.Text).AppendLine();
sb.AppendFormat("NumberOfThreads = {0}", txtNumOfThreads.Text).AppendLine();
// File.WriteAllText(GetFileName(), sb.ToString());
Close();
}
}
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
var dialogResult = openFileDialog1.ShowDialog();
if (dialogResult == DialogResult.OK)
{
txtFilePath.Text = openFileDialog1.FileName;
}
}
private void txtNumOfThreads_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
private void txtNumOfThreads_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.C:
case Keys.P:
case Keys.X:
e.Handled = true;
txtNumOfThreads.SelectionLength = 0;
break;
}
}
}
private void txtNumOfThreads_Enter(object sender, EventArgs e)
{
Clipboard.Clear();
}
private void SetDirty()
{
btnOk.Enabled = true;
}
private void chkLoggin_CheckedChanged(object sender, EventArgs e)
{
SetDirty();
}
private void txtFilePath_TextChanged_1(object sender, EventArgs e)
{
SetDirty();
}
private void txtNumOfThreads_TextChanged(object sender, EventArgs e)
{
SetDirty();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
public static object GetFileName()
{
string app_name = "SettingsFile";
string name = "";
object default_value = "";
// var app_name = "SettingsFile";
RegistryKey reg_key = Registry.LocalMachine.OpenSubKey("Software", true);
RegistryKey sub_key = reg_key.CreateSubKey(app_name);
return sub_key.GetValue(name, default_value);
}
private Boolean Validate()
{
bool isValid = false;
if (string.IsNullOrEmpty(txtFilePath.Text))
{
txtFilePath.BackColor = Color.Red;
}
else if (Regex.IsMatch(txtFilePath.Text, @"^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\"";|/]+$"))
{
txtFilePath.BackColor = Color.Red;
}
else
{
txtFilePath.BackColor = DefaultBackColor;
isValid = true;
}
return isValid;
}
}
}