2012年2月8日 星期三

022-讀寫Regedit 方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Regedit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //讀取測試
        private void button1_Click(object sender, EventArgs e)
        {
            string subKeyPath = @"Software\Microsoft\Internet Account Manager\Accounts\00000005";
            string keyName = "POP3 User Name";
            if (HaveReg(subKeyPath))
            {
                MessageBox.Show(ReadReg(subKeyPath, keyName));
            }
            else
            {
                MessageBox.Show(subKeyPath + "路徑不存在");
            }
        }

        //寫入測試
        private void button2_Click(object sender, EventArgs e)
        {
            string subKeyPath = @"Software\Microsoft\Internet Account Manager\Accounts\00000005";
            string keyName = "POP3 User Password";
            string keyValue = "Andy";
            if (WriteReg(subKeyPath, keyName, keyValue))
            {
                MessageBox.Show("寫入成功");
            }
            else
            {
                MessageBox.Show("寫入失敗");
            }
        }

        // 讀取 Regedit
        public static string ReadReg(string SubKeyPath, string keyName)
        {
        string keyValue = "";

        try
        {
            RegistryKey rootKey = Registry.CurrentUser; //Registry參數
            RegistryKey subKey = rootKey.OpenSubKey(SubKeyPath);
            keyValue = subKey.GetValue(keyName).ToString();
            subKey.Close();
            rootKey.Close();
        }
        catch (Exception)
        {
        }
        return keyValue;
        }

        public static Boolean HaveReg(string keyPath)
        {
            Boolean returnValue = false;
            try
            {
                RegistryKey rootKey = Registry.CurrentUser;
                RegistryKey subKey = rootKey.OpenSubKey(@keyPath);

                //不存在
                if (subKey == null)
                {
                    returnValue = false;
                }
                else
                {
                    returnValue = true;
                }
                subKey.Close();
                rootKey.Close();
            }
            catch (Exception)
            {
            }
            return returnValue;
        }

        // 寫入 Regedit
        public static Boolean WriteReg(string subKeyPath, string keyName, string keyValue)
        {
            Boolean returnValue = false;
            try
            {
                RegistryKey rootKey = Registry.CurrentUser;
                RegistryKey subKey = rootKey.OpenSubKey(@subKeyPath);

                //不存在,Create
                if (subKey == null)
                {
                rootKey.CreateSubKey(subKeyPath);
                }

                //寫入資料
                subKey = rootKey.OpenSubKey(subKeyPath, true);
                subKey.SetValue(keyName, keyValue);
                returnValue = true;
                subKey.Close();
                rootKey.Close();
            }
            catch (Exception)
            {
            }
            return returnValue;
        }

    }
}