• wpf读写注册表


    WPF,C# 里 读写注册表的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.Win32;
    namespace FTMedDicomScpServer
    {
        /// <summary>
        /// 注册表参数读取的单例类
        /// </summary>
        public class ParameterSingleFactory
        {
            private static ParameterSingleFactory parameterInstance = null;
            private static readonly object lockHelper = new object();
    
            private ParameterSingleFactory()
            {
                GetParametersFromReg();
            }
            public void GetParametersFromReg()
            {
                RegistryKey rkey = Registry.LocalMachine;
    
                RegistryKey rkey1 = rkey.OpenSubKey("SOFTWARE\WOW6432Node", true);
                if (rkey1==null)
                    rkey1 = rkey.OpenSubKey("SOFTWARE", true);
                if (rkey1 != null)
                {
                    RegistryKey rkey2 = rkey1.OpenSubKey("FTMedDicomScpServer", true);
                    RegistryKey rkey3;
                    if (rkey2 == null)
                    {
                        try
                        {
                            rkey2 = rkey1.CreateSubKey("FTMedDicomScpServer");
                            rkey3 = rkey2.CreateSubKey("FTMedDicomScpService");
                            rkey3.SetValue("DicomScpAE", DicomScpAE);
                            rkey3.SetValue("DicomScpPort", DicomScpPort);
                            rkey3.SetValue("DicomScpIp", DicomScpIP);
                            rkey3.SetValue("DicomScpSavePath", DicomScpPath);
                            rkey2.Close();
                        }
                        catch (Exception e)
                        {
                           
                        }
                    }
                    else
                    {
                        rkey3 = rkey2.OpenSubKey("FTMedDicomScpService", true);
                        if (rkey3 == null)
                        {
                            try
                            {
                                rkey3 = rkey2.CreateSubKey("FTMedDicomScpService");
                                rkey3.SetValue("DicomScpAE", DicomScpAE);
                                rkey3.SetValue("DicomScpPort", DicomScpPort);
                                rkey3.SetValue("DicomScpIp", DicomScpIP);
                                rkey3.SetValue("DicomScpSavePath", DicomScpPath);
                                rkey3.Close();
                            }
                            catch (Exception e)
                            {
    
                            }
                            
                        }
                        else
                        {
                            if (rkey3.GetValue("DicomScpAE") == null){
                                rkey3.SetValue("DicomScpAE", "SCPserver");}
                            else{
                                this.m_strDicomScpAE = rkey3.GetValue("DicomScpAE").ToString();}
    
                            if (rkey3.GetValue("DicomScpPort") == null){
                                rkey3.SetValue("DicomScpPort", "3333");}
                            else{
                                this.m_strDicomScpPort = rkey3.GetValue("DicomScpPort").ToString(); }
    
                            if (rkey3.GetValue("DicomScpIp") == null){
                                rkey3.SetValue("DicomScpIp", "localhost");}
                            else{
                                this.m_strDicomScpIP = rkey3.GetValue("DicomScpIp").ToString();}
    
                            if (rkey3.GetValue("DicomScpSavePath")== null) {
                                rkey3.SetValue("DicomScpSavePath", "D:\storeSCP");}
                            else {
                                this.m_strDicomScpPath = rkey3.GetValue("DicomScpSavePath").ToString();}
    
                            rkey3.Close();
                        }
                        rkey2.Close();
                    }
                }
                rkey1.Close(); 
            }
    
            public void SetParametersToReg()
            {
                RegistryKey rkey = Registry.LocalMachine;
                RegistryKey rkey1 = rkey.OpenSubKey("SOFTWARE\WOW6432Node", true);
                if (rkey1 == null)
                    rkey1 = rkey.OpenSubKey("SOFTWARE", true);
                RegistryKey rkey2 = rkey1.CreateSubKey("FTMedDicomScpServer");
                RegistryKey rkey3 = rkey2.CreateSubKey("FTMedDicomScpService");
    
                rkey3.SetValue("DicomScpAE", this.m_strDicomScpAE);
                rkey3.SetValue("DicomScpPort", this.m_strDicomScpPort);
                rkey3.SetValue("DicomScpIp", this.m_strDicomScpIP);
                rkey3.SetValue("DicomScpSavePath", this.m_strDicomScpPath);
                rkey3.Close();
                rkey2.Close();
                rkey1.Close(); 
            }
            public void SetParamToReg(string serviceName, string key,string value)
            {
                RegistryKey rkey = Registry.LocalMachine;
                RegistryKey rkey1 = rkey.OpenSubKey("SOFTWARE\WOW6432Node", true);
                if (rkey1 == null)
                    rkey1 = rkey.OpenSubKey("SOFTWARE", true);
                RegistryKey rkey2 = rkey1.OpenSubKey("FTMedDicomScpServer\" + serviceName, true);
    
                if (rkey2 == null)
                {
                    RegistryKey rkey3 = rkey.OpenSubKey("FTMedDicomScpService", true);
                    if (rkey3 == null)
                    {
                        rkey3 = rkey1.CreateSubKey("FTMedDicomScpServer");
                    }
                    rkey2 = rkey3.CreateSubKey(serviceName);
                    rkey3.Close();
                }
                rkey2.SetValue(key,value);
                rkey2.Close();
                rkey1.Close();
                rkey.Close();
            }
             public string GetParamFromReg(string serviceName,string key)
            {
                string value = null;
    
                RegistryKey rkey = Registry.LocalMachine;
                //The   second   parameter   tells   it   to   open   the   key   as   writable 
                RegistryKey rkey1 = rkey.OpenSubKey("SOFTWARE\FTMedDicomScpServer\" + serviceName, true);
    
                if (rkey1 == null)
                {
                    rkey1 = rkey.OpenSubKey("SOFTWARE\WOW6432Node\FTMedDicomScpServer\" + serviceName, true);
                }
                if (rkey1 != null)
                {
                    value = (string)(rkey1.GetValue(key, "0"));
                }
                else return value;
    
                rkey1.Close();
                return value;
            }
        
    
            public static ParameterSingleFactory CreateInstance()
            {
                if (parameterInstance == null)
                {
                    lock (lockHelper)
                    {
                        if (parameterInstance == null)
                            parameterInstance = new ParameterSingleFactory();
                    }
                }
                return parameterInstance;
            }
    
            private string m_strDicomScpAE;
            private string m_strDicomScpPort;
            private string m_strDicomScpIP;
            private string m_strDicomScpPath;
    
            public string DicomScpAE
            {
                get { return this.m_strDicomScpAE; }
                set { this.m_strDicomScpAE = value; }
            }
    
            public string DicomScpPort
            {
                get { return this.m_strDicomScpPort; }
                set { this.m_strDicomScpPort = value; }
            }
    
            public string DicomScpIP
            {
                get { return this.m_strDicomScpIP; }
                set { this.m_strDicomScpIP = value; }
            }
    
            public string DicomScpPath
            {
                get { return this.m_strDicomScpPath; }
                set { this.m_strDicomScpPath = value; }
            }
        }
    }
  • 相关阅读:
    【iCore3 双核心板_FPGA】实验二十六:SDRAM读写测试实验
    【iCore3 双核心板_FPGA】实验二十五:NIOS II之UART串口通信实验
    【iCore3 双核心板_FPGA】实验二十四:Niosii——SDRAM读写实验
    【iCore3 双核心板】iCore3封装库及使用说明V1.0
    【iCore3 双核心板_FPGA】实验二十三:使用JTAG UART终端打印信息
    【iCore3 双核心板_FPGA】实验二十二:Niosii——固化程序到 EPCS 里
    【7集iCore3基础视频】7-5 iTool2驱动安装
    【iCore3 双核心板_FPGA】实验二十:基于FIFO的ARM+FPGA数据存取实验
    【iCore3 双核心板_FPGA】实验二十一:Niosii——基于内部RAM建立第一个软核
    【7集iCore3基础视频】7-4 iCore3连接示意图
  • 原文地址:https://www.cnblogs.com/LILING3/p/7346780.html
Copyright © 2020-2023  润新知