软件学习记录(七)配置文件的保存与读取功能(ini文件的使用)

软件学习记录(七)配置文件的保存与读取功能(ini文件的使用)

配置文件路径:

CommonMethods.SysSetPath = Path.Combine(new string[] { Application.StartupPath.ToString(), "Settings", "SysSet.ini" });

(一)读取功能:

把读取功能放到一个静态类中

public static class CommonMethods
    {
        /// 系统配置文件路径
        public static string SysSetPath = string.Empty;

        /// <summary>
        /// 读取系统配置文件
        /// </summary>
        /// <returns></returns>
        public static ConfigInfoExt LoadSettings()
        {
            ConfigInfoExt objConfig = new ConfigInfoExt();

            try
            {
                //自动运动参数
                objConfig.LiftZAxis = Convert.ToInt32(IniConfigHelper.ReadIniData("自动运行轨迹", "上升Z轴", "", SysSetPath));

                return objConfig;
            }
            catch (Exception)
            {
                //写入
                return null;
            }
        }
    }

模型类如下:

namespace Models
{
    public class ConfigInfo
    {
        //初始速度
        public double VelMin { get; set; }
    }
}

调用的时候,返回这个模型的实例,选择更新到UI界面上

ConfigInfo objConfig = CommonMethods.LoadSettings();

存储功能:

		private bool SaveIniConfig()
        {
            // 判断文件是否存在
            if (!File.Exists(CommonMethods.SysSetPath))
            {
                FileStream fs = new FileStream(CommonMethods.SysSetPath, FileMode.Create);
                fs.Close();
            }
            bool result = true;

            result &= IniConfigHelper.WriteIniData("自动运动参数", "初始速度", this.num_MinVel.Value.ToString(), CommonMethods.SysSetPath);
            return result;
        }
posted @ 2023-03-15 15:49  聆听微风  阅读(170)  评论(0编辑  收藏  举报