Nithin B
Nithin B

Reputation: 680

Can't find Keys in Registry Editor created using c# though I can read using C# code

I am trying to save configurations in windows application to the registry using c#. I can successfully write to the registry and retrieve back using c# code. But when I look for them in registry editor I can't find them.

I writing to the registry using below code:

private void BtnAddSchedule_Click(object sender, EventArgs e)
{
    try
    {
        if (txtScheduleName.Text != string.Empty)
        {
            if (txtFileToSchedule.Text != string.Empty)
            {
                RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software", true).CreateSubKey("Scheduler.Manager", true);
                if (registryKey != null)
                {
                    RegistryKey registryKeyScheduleName = registryKey.CreateSubKey(txtScheduleName.Text);
                    if (registryKeyScheduleName != null)
                    {
                        registryKeyScheduleName.SetValue("ScheduleName", txtScheduleName.Text);
                        registryKeyScheduleName.SetValue("FileToSchedule", txtFileToSchedule.Text);
                        registryKeyScheduleName.SetValue("Hour", nudScheduleHour.Value);
                        registryKeyScheduleName.SetValue("Minute", nudScheduleMinute.Value);
                        registryKeyScheduleName.Close();

                        UpdateScheduleList();
                        btnAddSchedule.Text = "Add";
                    }
                    else
                    {
                        throw new Exception("Unable to create registry key '" + txtScheduleName.Text + "'.");
                    }
                    registryKey.Close();
                }
                else
                {
                    throw new Exception("Unable to create registry key 'Scheduler.Manager'.");
                }
            }
            else
            {
                MessageBox.Show(text: "Please select valid file to schedule.", caption: "Error Info", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show(text: "Please enter valid schedule name.", caption: "Error Info", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
        }
    }
    catch (Exception exception)
    {
        MessageBox.Show(text: exception.Message, caption: "Error Info", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
    }
}

I am reading registry keys using below code:

private void UpdateScheduleList()
{
    lvScheduleList.Items.Clear();

    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software", false).OpenSubKey("Scheduler.Manager", false);
    if (registryKey != null)
    {
        string[] registryKeyScheduleNameNameList = registryKey.GetSubKeyNames();

        RegistryKey registryKeyScheduleName;
        ListViewItem listViewItem = null;
        foreach (string registryKeyScheduleNameName in registryKeyScheduleNameNameList)
        {
            registryKeyScheduleName = registryKey.OpenSubKey(registryKeyScheduleNameName);
            if (registryKeyScheduleName != null && registryKeyScheduleName.ValueCount == 4)
            {
                listViewItem = lvScheduleList.Items.Add(registryKeyScheduleNameName);
                listViewItem.SubItems.Add(registryKeyScheduleName.GetValue("FileToSchedule").ToString());
                listViewItem.SubItems.Add(DateTime.Now.Date.AddHours(Convert.ToInt32(registryKeyScheduleName.GetValue("Hour").ToString())).AddMinutes(Convert.ToInt32(registryKeyScheduleName.GetValue("Minute").ToString())).ToString());
                registryKeyScheduleName.Close();
            }
            else
            {
                continue;
            }
        }
        registryKey.Close();
    }
}

When I inspect the path of the registry key, I get the below path but can't find 'Scheduler.Manager' key in Registry editor

{HKEY_LOCAL_MACHINE\Software\Scheduler.Manager\Schedule1}

Is there anything I am doing wrong here?

Upvotes: 1

Views: 437

Answers (1)

Flydog57
Flydog57

Reputation: 7111

Is your app running as a 32-bit app on a 64-box? That will result in your programmatic access to the registry being redirected to the 32-bit section of the registry (the WOW node). If this is the case, you can see the results using the 32-bit registry editor.

This can happen because you are specifying that you are building a 32-bit application, or because you are targeting "Any" platform, but have the "Prefer 32-bit" checkbox checked.

Upvotes: 2

Related Questions