zoukankan      html  css  js  c++  java
  • wpf中读写配置文档的方法

    WPF配置文件读取与写入:

    1、 在程序根目录下新建文本文档,将后缀名改成.ini文件;(将已存在的Param.dat的后缀改成。ini

    譬如:Param.ini

    1) 文档的写法:INI文件由节、键、值组成。

    参考链接:(https://www.cnblogs.com/renyuan/p/4111695.html)

     

    2、 VS项目中新建类:INIFile.cs

     

        class INIFile

        {

            private string filePath;

            [DllImport("kernel32")]

            private static extern long WritePrivateProfileString(string strSection, string strKey, string strVal, string strFilePath);

            

            [DllImport("kernel32")]

            private static extern int GetPrivateProfileString(string strSection, string strKey, string strDef, StringBuilder sbRetVal, int intSize, string strFilePath);

            

            public INIFile(string strFilePath)

            {

                this.filePath = strFilePath;

            }           

            

            public void Write(string strSection, string strKey, string strValue)

            {

                WritePrivateProfileString(strSection, strKey, strValue, this.filePath);

            }

            public string Read(string strSection, string strKey)

            {

                StringBuilder sb = new StringBuilder(255);

                int i = GetPrivateProfileString(strSection, strKey, "", sb, 255, this.filePath);

                return sb.ToString();

            }                   

            public string FilePath

            {

                get { return this.filePath; }

                set { this.filePath = value; }

            }

        }

    3、 调用类方法:

    //定义

    INIFile INI_Param = new INIFile(Environment.CurrentDirectory + @"Param.ini");

    //读取配置文档参数

    private bool ReadParam()

            {

                try

                {

                    float buff;

                    //ActionLog.WriteLog("Read Setting Information...");

                    buff = Convert.ToSingle(INI_Param.Read("SysParam", "Range"));

                    rangeTb.Text = buff.ToString();

                    buff = Convert.ToSingle(INI_Param.Read("SysParam", "Grade"));

                    comboBox.Text = buff.ToString();

                    buff = Convert.ToSingle(INI_Param.Read("SysParam", "MarginError"));

                    marginErrorTxt.Text = buff.ToString();                         

                    return true;

                }

                catch(Exception ex)

                {

                    ErrorLog.WriteLog("读取参数失败:" + ex.Message.ToString());

                    return false;

                }

            }

    //读取工位信息

    private List<string> readStation()

            {

                List<string> stationInfo = new List<string>();            

                stationInfo.Add(INI_Param.Read("StationInfo", "Station1"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station2"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station3"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station4"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station5"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station6"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station7"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station8"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station9"));

                stationInfo.Add(INI_Param.Read("StationInfo", "Station10"));

                return stationInfo;

            }

    //将数据更新到配置文档中

            private void UpdateParam()

            {

                try

                {

                    string buff;

                    buff = rangeTb.Text;

                    INI_Param.Write("SysParam", "Range", buff);

                    buff = comboBox.Text;

                    INI_Param.Write("SysParam", "Grade", buff);

                    buff = marginErrorTxt.Text;

                    INI_Param.Write("SysParam", "MarginError", buff);

                }

                catch(Exception ex)

                {

                    ErrorLog.WriteLog("写入参数失败:" + ex.Message.ToString());

                }

            }

  • 相关阅读:
    从aop中获取被拦截方法中的参数
    使用多线程 执行有返回值的方法
    MyBatis中#{}和${}的区别
    Java 调用api,json化结果
    Spring入门
    实用: 将程序的内容写出到excel中
    实用:Java基础流计算
    2020年7月12号笔记
    2020年7月11号笔记
    2020年7月6号笔记
  • 原文地址:https://www.cnblogs.com/xiehaha123/p/11713387.html
Copyright © 2011-2022 走看看