在一些系统软件开发中,常常将一些软件运行环境、系统配置等信息写入到注册表中,除非是所谓的绿色软件。
在Windows平台上的软件,几乎都会和注册表打交道。在本节中,将介绍如何将信息写入到注册表中。
//首先包含如下引用
using Microsoft.Win32;
//写注册表
void SaveSettings()
{
RegistryKeySoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
RegistryKeyMovecontKey=SoftwareKey.CreateSubKey("Movecont");//建立
RegistryKeySelfPlaceKey=MovecontKey.CreateSubKey("SelfPlace");//建立
SelfPlaceKey.SetValue("BackColor",(object)BackColor.ToKnownColor());//写
SelfPlaceKey.SetValue("Red",(object)(int)BackColor.R);//红
SelfPlaceKey.SetValue("Green",(object)(int)BackColor.G);//绿
SelfPlaceKey.SetValue("Blue",(object)(int)BackColor.B);//蓝
SelfPlaceKey.SetValue("Width",(object)Width);//宽
SelfPlaceKey.SetValue("Height",(object)Height);//高
SelfPlaceKey.SetValue("X",(object)DesktopLocation.X);//左上角X坐标
SelfPlaceKey.SetValue("Y",(object)DesktopLocation.Y);//左上角Y坐标
SelfPlaceKey.SetValue("WindowState",(object)WindowState.ToString());//左上角Y坐标
}
//读注册表
bool ReadSettings()
{
RegistryKeySoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
RegistryKeyMovecontKey=SoftwareKey.OpenSubKey("Movecont");//建立
if(MovecontKey==null)
returnfalse;
RegistryKeySelfPlaceKey=MovecontKey.OpenSubKey("SelfPlace");//建立
if(SelfPlaceKey==null)
returnfalse;
else
this.listBoxMessages.Items.Add("成功打开注册表!");
intRed=(int)SelfPlaceKey.GetValue("Red");
intGreen=(int)SelfPlaceKey.GetValue("Green");
intBlue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);
this.listBoxMessages.Items.Add("BackcolorName:"+BackColor.Name);
intX=(int)SelfPlaceKey.GetValue("X");
intY=(int)SelfPlaceKey.GetValue("Y");
DesktopLocation=newPoint(X,Y);
this.listBoxMessages.Items.Add("Location:"+DesktopLocation.ToString());
Width=(int)SelfPlaceKey.GetValue("Width");
Height=(int)SelfPlaceKey.GetValue("Height");
this.listBoxMessages.Items.Add("Size:"+newSize(Width,Height).ToString());
stringInitstate=(string)SelfPlaceKey.GetValue("WindowState");
//****枚举类型数据的Parse
WindowState=(FormWindowState)FormWindowState.Parse(WindowState.GetType(),Initstate);
returntrue;
}