Base de connaissance
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le Deal du moment : -39%
Ordinateur portable ASUS Chromebook Vibe CX34 Flip
Voir le deal
399 €

registry operations

Aller en bas

registry operations Empty registry operations

Message  Admin Jeu 9 Juil - 10:12

Introduction
I've done a little update, now this class provides six functions:

•Read to read a registry key.
•Write to write into a registry key.
•DeleteKey to delete a registry key.
•DeleteSubKeyTree to delete a sub key and any child.
•SubKeyCount to retrieve the count of subkeys at the current key.
•ValueCount to retrieve the count of values in the key.
and three properties:

•ShowError to show or hide error messages (default = false).
•SubKey to set the subkey value (default = "SOFTWARE\\" + Application.ProductName).
•BaseRegistryKey to set the base registry key value (default = Registry.LocalMachine).
Source code
Importing other namespaces...

Collapse Copy Codeusing System;
// it's required for reading/writing into the registry:
using Microsoft.Win32;
// and for the MessageBox function:
using System.Windows.Forms;Read function
•input: the key name (string)
•output: value of the key (string)
Collapse Copy Codepublic 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;
}
}
}Write function
•input: the key name (string) and its value (object)
•output: true if OK or false if error
Collapse Copy Codepublic 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;
}
}DeleteKey function
•input: the key name (string)
•output: true if OK or false if error
Collapse Copy Codepublic 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;
}
}DeleteSubKeyTree function
•input: void
•output: true if OK or false if error
Collapse Copy Codepublic 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;
}
}SubKeyCount function
•input: void
•output: number of subkeys at the current key
Collapse Copy Codepublic 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;
}
}ValueCount function
•input: void
•output: number of values in the key
Collapse Copy Codepublic 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;
}
}ShowErrorMessage function
This is a private function to show the error message if the property showError = true.

Collapse Copy Codeprivate void ShowErrorMessage(Exception e, string Title)
{
if (showError == true)
MessageBox.Show(e.Message,
Title
,MessageBoxButtons.OK
,MessageBoxIcon.Error);
}Using the code
First of all, you have to import its namespace:

Collapse Copy Codeusing Utility.ModifyRegistry;and to instantiate your class:

Collapse Copy CodeModifyRegistry myRegistry = new ModifyRegistry();The three properties have already their default values. However, you can modify them:

property default value example
ShowError false myRegistry.ShowError = true;
BaseRegistryKey Registry.LocalMachine myRegistry.BaseRegistryKey = Registry.CurrentUser;
SubKey "SOFTWARE\\" + Application.ProductName myRegistry.SubKey = "SOFTWARE\\MYPROGRAM\\MYSUBKEY";

OK, now you can read, write, and delete from your registry.

To read:

Collapse Copy CodemyRegistry.Read("MY_KEY");Note: if MY_KEY doesn't exist, the Read function will return null.

To write:

Collapse Copy CodemyRegistry.Write("MY_KEY", "MY_VALUE");Note: if the SubKey doesn't exist, it will be automatically created.

To delete a single key:

Collapse Copy CodemyRegistry.DeleteKey("MY_KEY");Note: also if the MY_KEY doesn't exist, this function will return TRUE.

To delete the subkey tree:

Collapse Copy CodemyRegistry.DeleteSubKeyTree();Note: this code will delete the SubKey and all its children.

To retrieve the count of subkeys at the current key:

Collapse Copy CodemyRegistry.SubKeyCount();To retrieve the count of values in the key:

Collapse Copy CodemyRegistry.ValueCount();Example
The following code is an example from my in progress RTF file editor program. This code retrieves the "Recent files" list from registry:

Collapse Copy Code[...]
ModifyRegistry myRegistry = new ModifyRegistry();
myRegistry.SubKey = "SOFTWARE\\RTF_SHARP_EDIT\\RECENTFILES";
myRegistry.ShowError = true;
int numberValues = myRegistry.ValueCount();

for (int i = 0; i < numberValues; i++)
{
arrayRecentFiles[i] = myRegistry.Read(i.ToString());
[...]and this code writes the "Recent files" list into registry:

Collapse Copy CodeModifyRegistry myRegistry = new ModifyRegistry();
myRegistry.SubKey = "SOFTWARE\\RTF_SHARP_EDIT\\RECENTFILES";
myRegistry.ShowError = true;
myRegistry.DeleteSubKeyTree();
for (int i = 0; i < 8 ; i++)
{
if (arrayRecentFiles[i] == null)
break;
myRegistry.Write(i.ToString(), arrayRecentFiles[i]);
}Conclusion
The .NET framework has a lot of functions to read and modify the registry, my class is only a start point. I hope this helps!

Admin
Admin

Nombre de messages : 45
Date d'inscription : 20/01/2009

http://programming.formyjob.net

Revenir en haut Aller en bas

Revenir en haut

- Sujets similaires

 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum