C# 操作注册表
1
//首先包含如下引用
2
3
using Microsoft.Win32;
4
5
//写注册表
6
7
void SaveSettings()
8
9
{
10
11
RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
12
13
RegistryKey MovecontKey=SoftwareKey.CreateSubKey("Movecont");//建立
14
15
RegistryKey SelfPlaceKey=MovecontKey.CreateSubKey("SelfPlace");//建立
16
17
SelfPlaceKey.SetValue("BackColor",(object)BackColor.ToKnownColor());//写
18
19
SelfPlaceKey.SetValue("Red",(object)(int)BackColor.R);//红
20
21
SelfPlaceKey.SetValue("Green",(object)(int)BackColor.G);//绿
22
23
SelfPlaceKey.SetValue("Blue",(object)(int)BackColor.B);//蓝
24
25
SelfPlaceKey.SetValue("Width",(object)Width);//宽
26
27
SelfPlaceKey.SetValue("Height",(object)Height);//高
28
29
SelfPlaceKey.SetValue("X",(object)DesktopLocation.X);//左上角X坐标
30
31
SelfPlaceKey.SetValue("Y",(object)DesktopLocation.Y);//左上角Y坐标
32
33
SelfPlaceKey.SetValue("WindowState",(object)WindowState.ToString());//左上角Y坐标
34
35
36
37
}
38
39
//读注册表
40
41
bool ReadSettings()
42
43
{
44
45
RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);
46
47
RegistryKey MovecontKey=SoftwareKey.OpenSubKey("Movecont");//建立
48
49
if(MovecontKey==null)
50
51
return false;
52
53
RegistryKey SelfPlaceKey=MovecontKey.OpenSubKey("SelfPlace");//建立
54
55
if(SelfPlaceKey==null)
56
57
return false;
58
59
else
60
61
this.listBoxMessages.Items.Add("成功打开注册表!");
62
63
int Red=(int)SelfPlaceKey.GetValue("Red");
64
65
int Green=(int)SelfPlaceKey.GetValue("Green");
66
67
int Blue=(int)SelfPlaceKey.GetValue("Blue");
68
69
BackColor=Color.FromArgb(Red,Green,Blue);
70
71
this.listBoxMessages.Items.Add("Backcolor Name:"+BackColor.Name);
72
73
int X=(int)SelfPlaceKey.GetValue("X");
74
75
int Y=(int)SelfPlaceKey.GetValue("Y");
76
77
DesktopLocation=new Point(X,Y);
78
79
this.listBoxMessages.Items.Add("Location:"+DesktopLocation.ToString());
80
81
Width=(int)SelfPlaceKey.GetValue("Width");
82
83
Height=(int)SelfPlaceKey.GetValue("Height");
84
85
this.listBoxMessages.Items.Add("Size:"+new Size(Width,Height).ToString());
86
87
string Initstate=(string)SelfPlaceKey.GetValue("WindowState");
88
89
//****枚举类型数据的Parse
90
91
WindowState=(FormWindowState)FormWindowState.Parse(WindowState.GetType(),Initstate);
92
93
return true;
94
95
}
96
//首先包含如下引用2

3
using Microsoft.Win32;4

5
//写注册表6

7
void SaveSettings()8

9
{10

11
RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);12

13
RegistryKey MovecontKey=SoftwareKey.CreateSubKey("Movecont");//建立14

15
RegistryKey SelfPlaceKey=MovecontKey.CreateSubKey("SelfPlace");//建立16

17
SelfPlaceKey.SetValue("BackColor",(object)BackColor.ToKnownColor());//写18

19
SelfPlaceKey.SetValue("Red",(object)(int)BackColor.R);//红20

21
SelfPlaceKey.SetValue("Green",(object)(int)BackColor.G);//绿22

23
SelfPlaceKey.SetValue("Blue",(object)(int)BackColor.B);//蓝24

25
SelfPlaceKey.SetValue("Width",(object)Width);//宽26

27
SelfPlaceKey.SetValue("Height",(object)Height);//高28

29
SelfPlaceKey.SetValue("X",(object)DesktopLocation.X);//左上角X坐标30

31
SelfPlaceKey.SetValue("Y",(object)DesktopLocation.Y);//左上角Y坐标32

33
SelfPlaceKey.SetValue("WindowState",(object)WindowState.ToString());//左上角Y坐标34

35
36

37
}38

39
//读注册表40

41
bool ReadSettings()42

43
{44

45
RegistryKey SoftwareKey=Registry.LocalMachine.OpenSubKey("Software",true);46

47
RegistryKey MovecontKey=SoftwareKey.OpenSubKey("Movecont");//建立48

49
if(MovecontKey==null)50

51
return false;52

53
RegistryKey SelfPlaceKey=MovecontKey.OpenSubKey("SelfPlace");//建立54

55
if(SelfPlaceKey==null)56

57
return false;58

59
else60

61
this.listBoxMessages.Items.Add("成功打开注册表!");62

63
int Red=(int)SelfPlaceKey.GetValue("Red");64

65
int Green=(int)SelfPlaceKey.GetValue("Green");66

67
int Blue=(int)SelfPlaceKey.GetValue("Blue");68

69
BackColor=Color.FromArgb(Red,Green,Blue);70

71
this.listBoxMessages.Items.Add("Backcolor Name:"+BackColor.Name);72

73
int X=(int)SelfPlaceKey.GetValue("X");74

75
int Y=(int)SelfPlaceKey.GetValue("Y");76

77
DesktopLocation=new Point(X,Y);78

79
this.listBoxMessages.Items.Add("Location:"+DesktopLocation.ToString());80

81
Width=(int)SelfPlaceKey.GetValue("Width");82

83
Height=(int)SelfPlaceKey.GetValue("Height");84

85
this.listBoxMessages.Items.Add("Size:"+new Size(Width,Height).ToString());86

87
string Initstate=(string)SelfPlaceKey.GetValue("WindowState");88

89
//****枚举类型数据的Parse90

91
WindowState=(FormWindowState)FormWindowState.Parse(WindowState.GetType(),Initstate);92

93
return true; 94

95
}96

浙公网安备 33010602011771号