[C#] อ่าน/เขียน Registry ตอนที่ 3 (จัดการ Registry Value)

จากบทความที่สอง เราได้เห็นตัวอย่างการจัดการ Keys และ Subkeys กันแล้ว ต่อไปบทความนี้สอนการใช้งานจริง โดยผมได้เพิ่มคำสั่งที่จำเป็นเพิ่มเติม เพื่อการทำงานที่มีประสิทธิภาพมากยิ่งขึ้น โดยผมได้แยกกรณีที่จำเป็นต้องใช้ ดังนี้

กรณีที่ 1 : อ่าน Value จาก Registry โดยมีขั้นตอนการทำงาน

  • เปิดการเชือมต่อกับ Hives และ Subkey ต่างๆ
  • อ่านค่า Value แต่ละอันออกมา
  • ปิดการเชื่อมต่อ
private void readRegistryValue(string pStrRegistryPath)
{
	//เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
    RegistryKey baseRegistryKey = Registry.LocalMachine;
    //เปิด Sub Key ที่ต้องการตาม Path ในที่นี้ คือ
    RegistryKey registrySubKey = baseRegistryKey.OpenSubKey(pStrRegistryPath);
    try
    {
        //ตรวจสอบว่าพบ Key นี้ หรือไม่
        if (registrySubKey != null)
        {
			//อ่านข้อมูลจาก Registry Value "MANUFACTURER" มาแสดงผลใน Textbox txtManufacturer
            this.txtManufacturer.Text = registrySubKey.GetValue("MANUFACTURER").ToString();
		    //อ่านข้อมูลจาก Registry Value "MODEL"
			//และมีการตรวจสอบ ในกรณีที่ไม่พบข้อมูล Value "MODEL" ให้ Return คำว่า "ERROR Value Not Found" แทน
            this.txtModel.Text = registrySubKey.GetValue("MODEL","ERROR Value Not Found").ToString();
        }
    }
    catch(Exception ex)
    {
        //แสดง MessageBox เมื่อเกิด Exception
        MessageBox.Show(this, "Error occur: " + ex.Message, "System Message", MessageBoxButtons.OK);
    }
    finally
    {
        //ปิดการเชื่อมต่อ
        baseRegistryKey.Close();
    }
}

กรณีที่ 2 : เขียน Value และสร้าง Value โดยมีขั้นตอนการทำงาน

  • เปิดการเชือมต่อกับ Hives และ Subkey ต่างๆ โดยต้องมีการอนุญาติให้เขียนข้อมูลด้วย
  • เขียนค่า Value แต่ละอันออกมา โดยมีแต่ละแบบให้ศึกษา ดังนี้
    • แบบที่ 1: มี Registry Value ระบบอัปเดตค่าใหม่ลงไป ได้แก่ MANUFACTURER และ MODEL
    • แบบที่ 2: ไม่มี Registry Value ระบบ จะสร้าง Registry Value ขึ้นมาใหม่ และเก็บค่าลงไป ได้แก่ APPNAME และ APPDEV
  • ปิดการเชื่อมต่อ
private void writeRegistryValue(string pStrRegistryPath)
{
    //เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
    RegistryKey baseRegistryKey = Registry.LocalMachine;
    //เปิด Sub Key ที่ต้องการตาม Path และกำหนด writeable เป็น true เพื่ออนุญาติให้มีการเขียนข้อมูลลง หรือสร้าง Value ใหม่
    RegistryKey registrySubKey = baseRegistryKey.OpenSubKey(pStrRegistryPath, true);
    try
    {
        //ตรวจสอบว่าพบ Key นี้ หรือไม่
        if (registrySubKey != null)
        {
            //แบบที่ 1: มี Registry Value แล้ว
            registrySubKey.SetValue("MANUFACTURER", "TOSHIBA");
            registrySubKey.SetValue("MODEL", "L645");
            //แบบที่ 2: ไม่มี Registry Value แล้ว ระบบสร้าง Value ขึ้นมาใหม่ และเขียนค่าลงไป
            registrySubKey.SetValue("APPNAME", "DS_Jjewerly");
            registrySubKey.SetValue("APPDEV", "Chatri Ngambenchawong");
        }
    }
    catch(Exception ex)
    {
        //แสดง MessageBox เมื่อเกิด Exception
	MessageBox.Show(this, "Error occur: " + ex.Message, "System Message", MessageBoxButtons.OK);
    }
    finally
    {
        //ปิดการเชื่อมต่อ
        baseRegistryKey.Close();
    }
}

กรณีที่ 3 : ลบ Value โดยมีขั้นตอนการทำงาน

  • เปิดการเชือมต่อกับ Hives และ Subkey ต่างๆ โดยต้องมีการอนุญาติให้เขียนข้อมูลด้วย
  • ทำการลบ Value นั้น
    • แบบที่ 1: ลบแบบปกติ
    • แบบที่ 2: ลบ และกำหนดให้ระบบไม่แจ้ง Exception กรณีที่ไม่เจอ Registry Value นั้น
    • แบบที่ 3: ลบ และกำหนดให้ระบบแจ้ง Exception กรณีที่ไม่เจอ Registry Value นั้น
  • ปิดการเชื่อมต่อ
private void deleteRegistryValue(string pStrRegistryPath)
{
    //เปิด Registry ในส่วน HKEY_LOCAL_MACHINE
    RegistryKey baseRegistryKey = Registry.LocalMachine;
    //เปิด Sub Key ที่ต้องการตาม Path และกำหนด writeable เป็น true ถ้าโปรแกรมไม่มีค่านี้ สามารถสร้างเพิ่มได้เลย
    RegistryKey registrySubKey = baseRegistryKey.OpenSubKey(pStrRegistryPath, true);
    try
    {
        //ตรวจสอบว่าพบ Key นี้ หรือไม่
        if (registrySubKey != null)
        {
		 	        //แบบที่ 1: ลบข้อมูลจาก Registry
            registrySubKey.DeleteValue("ISEVALUATE");
			         //แบบที่ 2: ลบข้อมูลจาก Registry โดยกำหนดให้ไม่แจ้ง Error(โยน Exception) กรณีไม่พบ Value นั้น
	    registrySubKey.DeleteValue("REGISDATE", false);
			         //แบบที่ 3: ลบข้อมูลจาก Registry โดยกำหนดให้แจ้ง Error(โยน Exception) กรณีไม่พบ Value นั้น
            registrySubKey.DeleteValue("EXPIREDATE", true);
        }
    }
    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.