[Csharp]
Using Microsoft. Win32;
Using System;
Using System. Collections;
Using System. Reflection;
Using System. Runtime. CompilerServices;
Using System. Runtime. InteropServices;
Using System. Security;
Using System. Security. Permissions;
Using System. Text;
Using System. Threading;
Using System. Windows. Forms;
/* 2012.6.21
* Registry read/write configuration class V1.1
* By: Transmart www.2cto.com
* We provide software outsourcing services, focus on industrial control configuration software and hardware development, and get involved in network, database, and system programming.
* You are welcome to send emails.
* Please keep this statement when reprinting. Thank you.
*/
Namespace Main
{
Public class Class_Setting
{
Static string myAppName = null;
Private static bool IsNothing (object Expression)
{
Return (Expression = null );
}
Private static string FormRegKey (string sApp, string sSect)
{
If (IsNothing (sApp) | (sApp. Length = 0 ))
{
Return @ "Software \ Transmart ";
}
If (IsNothing (sSect) | (sSect. Length = 0 ))
{
Return (@ "Software \ Transmart \" + sApp );
}
Return (@ "Software \ Transmart \" + sApp + @ "\" + sSect );
}
Private static void CheckPathTransmart (string s)
{
If (s = null) | (s. Length = 0 ))
{
Throw new ArgumentException ("the parameter path is null! ");
}
}
/// <Summary>
/// Initialize the program name. initialize the program name first.
/// </Summary>
/// <Param name = "Appname"> </param>
Public static void IniSetting (string Appname)
{
MyAppName = Appname;
}
Public static void SaveSetting (string Section, string Key, string Setting)
{
SaveSetting (myAppName, Section, Key, Setting );
}
Public static void SaveSetting (string AppName, string Section, string Key, string Setting)
{
CheckPathTransmart (AppName );
CheckPathTransmart (Section );
CheckPathTransmart (Key );
String subkey = FormRegKey (AppName, Section );
RegistryKey key = Registry. LocalMachine. CreateSubKey (subkey );
If (key = null)
{
Throw new ArgumentException ("configuration cannot be created! ");
}
Try
{
Key. SetValue (Key, Setting );
}
Catch (Exception exception)
{
Throw exception;
}
Finally
{
Key. Close ();
}
}
Public static string GetSetting (string Section, string Key, string Default = "")
{
Return GetSetting (myAppName, Section, Key, Default );
}
Public static string GetSetting (string AppName, string Section, string Key, string Default = "")
{
Object obj2;
RegistryKey key = null;
CheckPathTransmart (AppName );
CheckPathTransmart (Section );
CheckPathTransmart (Key );
If (Default = null)
{
Default = "";
}
String name = FormRegKey (AppName, Section );
Try
{
Key = Registry. LocalMachine. OpenSubKey (name );
If (key = null)
{
Return Default;
}
Obj2 = key. GetValue (Key, Default );
}
Finally
{
If (key! = Null)
{
Key. Close ();
}
}
If (obj2 = null)
{
Return null;
}
If (! (Obj2 is string ))
{
Throw new ArgumentException ("parameter error! ");
}
Return (string) obj2;
}
Public static string [,] GetAllSettings (string Section)
{
Return GetAllSettings (myAppName, Section );
}
Public static string [,] GetAllSettings (string AppName, string Section)
{
CheckPathTransmart (AppName );
CheckPathTransmart (Section );
String name = FormRegKey (AppName, Section );
RegistryKey key = Registry. CurrentUser. OpenSubKey (name );
If (key = null)
{
Return null;
}
String [,] strArray = null;
Try
{
If (key. ValueCount = 0)
{
Return strArray;
}
String [] valueNames = key. GetValueNames ();
Int upperBound = valueNames. GetUpperBound (0 );
String [,] strArray3 = new string [upperBound + 1, 2];
Int num3 = upperBound;
For (int I = 0; I <= num3; I ++)
{
String str2 = valueNames [I];
StrArray3 [I, 0] = str2;
Object obj2 = key. GetValue (str2 );
If (obj2! = Null) & (obj2 is string ))
{
StrArray3 [I, 1] = obj2.ToString ();
}
}
StrArray = strArray3;
}
Catch (StackOverflowException exception)
{
Throw exception;
}
Catch (OutOfMemoryException exception2)
{
Throw exception2;
}
Catch (ThreadAbortException exception3)
{
Throw prediction3;
}
Catch (Exception)
{
}
Finally
{
Key. Close ();
}
Return strArray;
}
}
}
Author: vbvcde