[C#] อ่าน/เขียน Registry ตอนที่ 2 (จัดการ Keys และ SubKeys)

จากบทความแรก ผมได้พาผู้อ่านได้รู้จักกับ Registry และคำสั่งพื้นฐานกันมาบ้างแล้ว คราวนี้ผมสอนทำตัวอย่างที่ใช้จัดการ Keys และ Subkeys กันนะครับ(ใช้คำสั่งชุดเดียวกัน) โดยแยกเป็นกรณีต่างๆ ดังนี้

กรณีที่ 1: สร้าง SubKeys หรือ Keys

  • ใช้คำสั่ง CreateSubKey ตามตัวอย่าง Code ดังนี้
private void createRegistryKey(string pStrRegistryPath)
{
    try
    {
		//เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
		RegistryKey baseRegistryKey = Registry.LocalMachine;
		//เปิด Sub Key ที่ต้องการตาม Path ในที่นี้ คือ
		RegistryKey registrySubKey = baseRegistryKey.CreateSubKey(pStrRegistryPath);
    }
    catch(Exception ex)
    {
        //แสดง MessageBox เมื่อเกิด Exception
        MessageBox.Show(this, "Error occur: " + ex.Message, "System Message", MessageBoxButtons.OK);
    }
}

กรณีที่ 2: เปิดและเชื่อมต่อ SubKeys หรือ Keys 

  • ใช้คำสั่ง OpenSubKey ถ้าหากไม่มี SubKeys หรือ Keys นั้นอยู่จริง คำสั่งนี้ Return ค่า null ตาม Code ดังนี้
private void openRegistryKey(string pStrRegistryPath)
{
	//เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
	RegistryKey baseRegistryKey = Registry.LocalMachine;
    //เปิด Sub Key ที่ต้องการตาม Path ในที่นี้ คือ
    RegistryKey registrySubKey = baseRegistryKey.OpenSubKey(pStrRegistryPath);
    try
    {
        //ตรวจสอบว่าพบ Key นี้ หรือไม่
		if (registrySubKey != null)
        {
			MessageBox.Show(this, "Open Registry Keys/SubKeys Complete", "System Message", MessageBoxButtons.OK);
        }
    }
    catch(Exception ex)
    {
        //แสดง MessageBox เมื่อเกิด Exception
        MessageBox.Show(this, "Error occur: " + ex.Message, "System Message", MessageBoxButtons.OK);
    }
    finally
    {
        //ปิดการเชื่อมต่อ
        baseRegistryKey.Close();
    }
}

กรณีที่ 3: ลบ SubKeys หรือ Keys

  • ใช้คำสั่ง DeleteSubKey ซึ่งสามารถเขียนได้ 2 แบบ ได้ตาม Code ด้านล่าง
  • หมายเหตุ ถ้าหากต้องการลบ SubKeys และ Keys ย่อยๆ ให้ใช้คำสั่ง DeleteSubKeyTree แทน
private void deleteRegistryKey(string pStrRegistryPath)
{
	//เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
	RegistryKey baseRegistryKey = Registry.LocalMachine;
	//เปิด Sub Key ที่ต้องการตาม Path ในที่นี้ คือ
	RegistryKey registrySubKey = baseRegistryKey.OpenSubKey(pStrRegistryPath);

    try
    {
		//แบบที่ 1: ตรวจสอบ SubKeys หรือ Keys โดยมีการตรวจสอบว่า Key นี้มีอยู่จริง หรือไม่
		//ตรวจสอบว่าพบ Key นี้ หรือไม่
		if (registrySubKey != null)
        {
			baseRegistryKey.DeleteSubKey(pStrRegistryPath);
        }
		//แบบที่ 2: ไม่ต้องตรวจสอบ แต่กำหนด Boolean ว่าให้ Throw Exception หรือไม่ในกรณีที่ไม่เจอ SubKeys หรือ Key นั้นๆ
		//True = Throw Exception
		//False = ไม่ Throw Exception
		baseRegistryKey.DeleteSubKey(pStrRegistryPath,false);
    }
    catch(Exception ex)
    {
        //แสดง MessageBox เมื่อเกิด Exception
        MessageBox.Show(this, "Error occur: " + ex.Message, "System Message", MessageBoxButtons.OK);
    }
    finally
    {
        //ปิดการเชื่อมต่อ
        baseRegistryKey.Close();
    }
}

Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.