danielkelly
IS-IT--Management
Hi,
Im having some difficulties understanding how best to get around my problem. Basically inside a method I am checking for the existance of various Registry Keys to ensure the correct structure is in place. The actual Method below is called CheckRegistryKeys().
Here is a code Snippet :-
private void CheckRegistryKeys()
{
RegistryKey hkcu = Registry.LocalMachine;
try
{
//Opens the Required Registry Keys
RegistryKey hkSoftware = hkcu.OpenSubKey("Software", true);
RegistryKey hkGC = hkSoftware.OpenSubKey("GC", true);
//Check if the GC Key Exists
if (hkGC == null)
{
//The Key does not exist. This is the First Time Running the Program so Create the Default Registry Keys
RegistryKey hkNewGC = hkSoftware.CreateSubKey("GC");
//Now Create The Default Registry Keys and Values
RegistryKey hkNewDatabase = hkNewGC.CreateSubKey("Database");
//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();
//Restart The Method to load the new values
CheckRegistryKeys();
}
//Open The Database Sub Key
RegistryKey hkDatabase = hkGrindCash.OpenSubKey("Database",true);
//Check if the Database Sub Key Exists
if (hkDatabase == null)
{
//The Key does not Exist so Create it
RegistryKey hkDatabaseNew = hkGC.CreateSubKey("Database");
//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();
//Restart The Method to load the new values
CheckRegistryKeys();
}
//Get The Values From The Registry and Check That They Are Not Empty
string strDatabaseLocation = Convert.ToString( hkDatabase.GetValue("Server"));
string strUserName = Convert.ToString(hkDatabase.GetValue("Username"));
string strPassword = Convert.ToString(hkDatabase.GetValue("Password"));
}
catch (Exception exc)
{
mySQLFunctions.LogError(clsGlobalVariables.GlobalUserName, "frmSplashScreen", "CheckRegistryKeys", "An Error Occurred Whilst Checking The Registry Keys", exc.Message.ToString());
}
}
I thought this was a suitable way to do it and it does work however I always get an Object Not Set To Instance Error which my Error handler is handling when I have no Registry Keys (initially).
Debugging the App shows me that restarting the Method (CheckRegistryKeys()) is where it is falling over as once it passes the first restart, it moves to the second and when it finished the second it returns to the first probably because Im assuming that is where the operation branched off. It then throws and Object Instance Exception. I hope this makes sense.
Can anyone suggest a better way to do this or offer some assistance.
Thanks,
Daniel.
Im having some difficulties understanding how best to get around my problem. Basically inside a method I am checking for the existance of various Registry Keys to ensure the correct structure is in place. The actual Method below is called CheckRegistryKeys().
Here is a code Snippet :-
private void CheckRegistryKeys()
{
RegistryKey hkcu = Registry.LocalMachine;
try
{
//Opens the Required Registry Keys
RegistryKey hkSoftware = hkcu.OpenSubKey("Software", true);
RegistryKey hkGC = hkSoftware.OpenSubKey("GC", true);
//Check if the GC Key Exists
if (hkGC == null)
{
//The Key does not exist. This is the First Time Running the Program so Create the Default Registry Keys
RegistryKey hkNewGC = hkSoftware.CreateSubKey("GC");
//Now Create The Default Registry Keys and Values
RegistryKey hkNewDatabase = hkNewGC.CreateSubKey("Database");
//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();
//Restart The Method to load the new values
CheckRegistryKeys();
}
//Open The Database Sub Key
RegistryKey hkDatabase = hkGrindCash.OpenSubKey("Database",true);
//Check if the Database Sub Key Exists
if (hkDatabase == null)
{
//The Key does not Exist so Create it
RegistryKey hkDatabaseNew = hkGC.CreateSubKey("Database");
//Show The Database Location Screen
frmDatabaseSettings formDatabase = new frmDatabaseSettings();
formDatabase.ShowDialog();
//Restart The Method to load the new values
CheckRegistryKeys();
}
//Get The Values From The Registry and Check That They Are Not Empty
string strDatabaseLocation = Convert.ToString( hkDatabase.GetValue("Server"));
string strUserName = Convert.ToString(hkDatabase.GetValue("Username"));
string strPassword = Convert.ToString(hkDatabase.GetValue("Password"));
}
catch (Exception exc)
{
mySQLFunctions.LogError(clsGlobalVariables.GlobalUserName, "frmSplashScreen", "CheckRegistryKeys", "An Error Occurred Whilst Checking The Registry Keys", exc.Message.ToString());
}
}
I thought this was a suitable way to do it and it does work however I always get an Object Not Set To Instance Error which my Error handler is handling when I have no Registry Keys (initially).
Debugging the App shows me that restarting the Method (CheckRegistryKeys()) is where it is falling over as once it passes the first restart, it moves to the second and when it finished the second it returns to the first probably because Im assuming that is where the operation branched off. It then throws and Object Instance Exception. I hope this makes sense.
Can anyone suggest a better way to do this or offer some assistance.
Thanks,
Daniel.