| 
 | 
 
[code=csharp]这里有一个关于注册表的简单实用,跟大家一起分享学习下 
图一为窗体打开的位置  
图二为窗体拖动到的位置并在此位置关闭了窗体,当窗体在此打开是窗体时在图二位置启动打开的,我们平时用的很多软件中有很多这个样子的,也就是注册表的读取和写入(RegistryKey类下的 GetVaule 和 SetValue) 
1.首先引用 using Microsoft.Win32; 此可以使用 RegistryKey 类 
2.Load事件从注册表读数据 
private void Form1_Load(object sender, EventArgs e) 
        { 
            RegistryKey regisOne; 
            RegistryKey regisTwo;//声明注册表对象 
            regisOne = Registry.CurrentUser;//获取当前用户的注册表基项 
            try 
            { 
            regisTwo = regisOne.CreateSubKey("Software\\MySoft");//在注册表中创建子项 
                this.Location = new Point(Convert.ToInt16(regisOne.GetValue("one")), Convert.ToInt16(regisTwo.GetValue("two")));//设置窗体的显示位置 
            } 
            catch (Exception ex) 
            { 
 
                new Exception(ex.Message); 
            } 
        } 
备注:在第一次加载窗体的时候注册表里是读取不到值的,返回null 
3.窗体关闭事件向注册表写数据 
 
 
private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
        { 
            RegistryKey regisOne, regisTwo;//声明注册表对象 
            regisOne = Registry.CurrentUser;//获取当前用户的注册表基项 
            regisTwo = regisOne.CreateSubKey("Software\\MySoft");//在注册表中创建子项 
            try 
            { 
                regisOne.SetValue("one", this.Location.X.ToString());//将窗体当前X坐标写入注册表 
                regisTwo.SetValue("two", this.Location.Y.ToString());//将当前窗体的Y坐标写入注册表 
            } 
            catch (Exception ex) 
            { 
                new Exception(ex.Message); 
            } 
        } 
 
就这么简单,这样,窗体就可以在关闭的位置启动了[/code] 
 |   
 
 
 
 |