读取INI配置文件

调用windows系统文件,将C++库转为C# 函数

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class IniConfigHelper
    {
        #region API函数声明

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        //需要调用GetPrivateProfileString的重载
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        private static extern uint GetPrivateProfileStringA(string section, string key,
            string def, Byte[] retVal, int size, string filePath);

        #endregion

        #region 读Ini文件

        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else return String.Empty;
        }

        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
            if (OpStation == 0)
                return false;
            else return true;
        }
        #endregion

    }
}

using MS_Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    //另外一个类,读取ModbusTCP的信息
    public class SettingManager
    {

        public SettingManager(string path)
        {
            this.Path = path;
        }

        private string _path;

        private ModbusTCPEntity iniSets;

        public string Path { get => _path; set => _path = value; }

        #region MyRegion

        
        public ModbusTCPEntity LoadSysSettings()
        {
            try
            {
                iniSets = new ModbusTCPEntity
                {
                    Ip = IniConfigHelper.ReadIniData("配置信息", "IP地址", "127.0.0.1", _path),
                    Port = IniConfigHelper.ReadIniData("配置信息", "端口号", "502", _path)
                };
                return iniSets;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool SaveSysSettings(ModbusTCPEntity iniSets)
        {
            bool result = true;
            result &= IniConfigHelper.WriteIniData("配置信息", "IP地址", iniSets.Ip, _path);
            result &= IniConfigHelper.WriteIniData("配置信息", "端口号", iniSets.Port, _path);
            return result;
        }

        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MS_Entity
{
    public class ModbusTCPEntity
    {
        public string Ip { get; set; }
        public string Port { get; set; }
    }
}


private static string FilePath = Application.StartupPath + "\\ConfigFile\\";

SettingManager IniManager = new SettingManager(FilePath + "MODBUSTCP_.ini");

CommonMethods.objModbusTCPEntity = IniManager.LoadSysSettings();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值