欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用C#在注册表中保存信息的例子

程序员文章站 2023-12-18 16:28:46
写出来,方便记忆,以后要用,可以直接改改就能用。 复制代码 代码如下:using system;namespace backend{   ...

写出来,方便记忆,以后要用,可以直接改改就能用。

复制代码 代码如下:

using system;

namespace backend
{
    public class registrystorage
    {
        public static pagevisibility openafterstart()
        {
            microsoft.win32.registrykey registrykey;
            pagevisibility visibility = pagevisibility.visible;

            // hkcu\software\regeditstorage
            registrykey = microsoft.win32.registry.currentuser.opensubkey(@"software\registrystorage");
            if (registrykey != null)
            {
                visibility = (string)registrykey.getvalue("pagevisibility") == pagevisibility.hide.tostring() ?
                    pagevisibility.hide : pagevisibility.visible;
                registrykey.close();
            }

            return visibility;
        }

        public static void savebeforeexit(pagevisibility visibility)
        {
            microsoft.win32.registrykey registrykey;

            // hkcu\software\regeditstorage
            registrykey = microsoft.win32.registry.currentuser.createsubkey(@"software\registrystorage");
            registrykey.setvalue("pagevisibility", visibility.tostring());
            registrykey.close();
        }
    }

    public enum pagevisibility
    {
        visible,
        hide
    }
}

代码很容易理解,下面添加一点代码来测试一下。

复制代码 代码如下:

using system;
using system.diagnostics;

namespace backend.test
{
    class program
    {
        static void main(string[] args)
        {
            // 存值
            registrystorage.savebeforeexit(pagevisibility.hide);
            // 取值
            pagevisibility visibility = registrystorage.openafterstart();
            // 验证
            debug.assert(visibility == pagevisibility.hide);

  
            // 存值
            registrystorage.savebeforeexit(pagevisibility.visible);
            // 取值
            visibility = registrystorage.openafterstart();
            // 验证
            debug.assert(visibility == pagevisibility.visible);

            console.writeline("press any key to exit.");
            console.readkey();
        }
    }
}

最后,上一张修改后的注册表截图:
使用C#在注册表中保存信息的例子

上一篇:

下一篇: