Click here to Skip to main content
15,896,346 members
Home / Discussions / C#
   

C#

 
GeneralText editor using c# Pin
shajuMathew18-May-04 1:22
shajuMathew18-May-04 1:22 
GeneralRe: Text editor using c# Pin
Heath Stewart18-May-04 4:04
protectorHeath Stewart18-May-04 4:04 
GeneralRe: Text editor using c# Pin
leppie18-May-04 7:11
leppie18-May-04 7:11 
Questionhow to run the algorithm whose code is given in c# Pin
gcmishra18-May-04 1:08
gcmishra18-May-04 1:08 
AnswerRe: how to run the algorithm whose code is given in c# Pin
Colin Angus Mackay18-May-04 1:17
Colin Angus Mackay18-May-04 1:17 
GeneralAdding and Retriving Values From Registry Pin
Anonymous18-May-04 1:07
Anonymous18-May-04 1:07 
GeneralRe: Adding and Retriving Values From Registry Pin
Aryadip18-May-04 1:22
Aryadip18-May-04 1:22 
GeneralRe: Adding and Retriving Values From Registry Pin
johnny.vilhjalmsson18-May-04 2:32
johnny.vilhjalmsson18-May-04 2:32 
I found this brilliant piece of code by Francesco Natali when I was looking for an easy way out.

/* ***************************************
* ModifyRegistry.cs
* ---------------------------------------
* a very simple class
* to read, write, delete and count
* registry values with C#
* ---------------------------------------
* if you improve this code
* please email me your improvement!
* ---------------------------------------
* by Francesco Natali
* - fn.varie@libero.it -
* ***************************************/

using System;
// it's required for reading/writing into the registry:
using Microsoft.Win32;
// and for the MessageBox function:
using System.Windows.Forms;

namespace Utility.ModifyRegistry
{
///
/// An useful class to read/write/delete/count registry keys
///

public class ModifyRegistry
{
private bool showError = false;
///
/// A property to show or hide error messages
/// (default = false)
///

public bool ShowError
{
get { return showError; }
set { showError = value; }
}

private string subKey = "SOFTWARE\\" + Application.ProductName.ToUpper();
///
/// A property to set the SubKey value
/// (default = "SOFTWARE\\" + Application.ProductName.ToUpper())
///

public string SubKey
{
get { return subKey; }
set { subKey = value; }
}

private RegistryKey baseRegistryKey = Registry.LocalMachine;
///
/// A property to set the BaseRegistryKey value.
/// (default = Registry.LocalMachine)
///

public RegistryKey BaseRegistryKey
{
get { return baseRegistryKey; }
set { baseRegistryKey = value; }
}

/* **************************************************************************
* **************************************************************************/

///
/// To read a registry key.
/// input: KeyName (string)
/// output: value (string)
///

public string Read(string KeyName)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey ;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistrySubKey doesn't exist -> (null)
if ( sk1 == null )
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
return null;
}
}
}

/* **************************************************************************
* **************************************************************************/

///
/// To write into a registry key.
/// input: KeyName (string) , Value (object)
/// output: true or false
///

public bool Write(string KeyName, object Value)
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.CreateSubKey(subKey);
// Save the value
sk1.SetValue(KeyName.ToUpper(), Value);

return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
return false;
}
}

/* **************************************************************************
* **************************************************************************/

///
/// To delete a registry key.
/// input: KeyName (string)
/// output: true or false
///

public bool DeleteKey(string KeyName)
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.CreateSubKey(subKey);
// If the RegistrySubKey doesn't exists -> (true)
if ( sk1 == null )
return true;
else
sk1.DeleteValue(KeyName);

return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Deleting SubKey " + subKey);
return false;
}
}

/* **************************************************************************
* **************************************************************************/

///
/// To delete a sub key and any child.
/// input: void
/// output: true or false
///

public bool DeleteSubKeyTree()
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistryKey exists, I delete it
if ( sk1 != null )
rk.DeleteSubKeyTree(subKey);

return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Deleting SubKey " + subKey);
return false;
}
}

/* **************************************************************************
* **************************************************************************/

///
/// Retrive the count of subkeys at the current key.
/// input: void
/// output: number of subkeys
///

public int SubKeyCount()
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistryKey exists...
if ( sk1 != null )
return sk1.SubKeyCount;
else
return 0;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Retriving subkeys of " + subKey);
return 0;
}
}

/* **************************************************************************
* **************************************************************************/

///
/// Retrive the count of values in the key.
/// input: void
/// output: number of keys
///

public int ValueCount()
{
try
{
// Setting
RegistryKey rk = baseRegistryKey ;
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistryKey exists...
if ( sk1 != null )
return sk1.ValueCount;
else
return 0;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Retriving keys of " + subKey);
return 0;
}
}

/* **************************************************************************
* **************************************************************************/

private void ShowErrorMessage(Exception e, string Title)
{
if (showError == true)
MessageBox.Show(e.Message,
Title
,MessageBoxButtons.OK
,MessageBoxIcon.Error);
}
}
}


JV
GeneralCustom project and custom code pages Pin
narada10818-May-04 1:07
narada10818-May-04 1:07 
GeneralRe: Custom project and custom code pages Pin
Dave Kreskowiak18-May-04 2:34
mveDave Kreskowiak18-May-04 2:34 
GeneralRe: Custom project and custom code pages Pin
Heath Stewart18-May-04 4:11
protectorHeath Stewart18-May-04 4:11 
GeneralRe: Custom project and custom code pages Pin
narada10818-May-04 5:40
narada10818-May-04 5:40 
GeneralRe: Custom project and custom code pages Pin
Heath Stewart18-May-04 5:46
protectorHeath Stewart18-May-04 5:46 
GeneralRe: Custom project and custom code pages Pin
narada10818-May-04 6:14
narada10818-May-04 6:14 
GeneralRe: Custom project and custom code pages Pin
Heath Stewart18-May-04 6:22
protectorHeath Stewart18-May-04 6:22 
GeneralRe: Custom project and custom code pages Pin
narada10818-May-04 6:29
narada10818-May-04 6:29 
GeneralRe: Custom project and custom code pages Pin
Heath Stewart18-May-04 6:39
protectorHeath Stewart18-May-04 6:39 
GeneralRe: Custom project and custom code pages Pin
narada10818-May-04 6:47
narada10818-May-04 6:47 
GeneralRe: Custom project and custom code pages Pin
Heath Stewart18-May-04 6:57
protectorHeath Stewart18-May-04 6:57 
GeneralRe: Custom project and custom code pages Pin
narada10818-May-04 7:01
narada10818-May-04 7:01 
GeneralRe: Custom project and custom code pages Pin
narada10819-May-04 2:55
narada10819-May-04 2:55 
GeneralWindows media encoder and server Pin
koala8118-May-04 0:24
koala8118-May-04 0:24 
GeneralRe: Windows media encoder and server Pin
Dave Kreskowiak18-May-04 2:29
mveDave Kreskowiak18-May-04 2:29 
GeneralPic in databse Pin
sreejith ss nair17-May-04 23:58
sreejith ss nair17-May-04 23:58 
GeneralRe: Pic in databse Pin
Heath Stewart18-May-04 4:21
protectorHeath Stewart18-May-04 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.