ini文件读写【C#】

作用:

【文本】存储到硬盘。

class对象【属性值】到硬盘。

txt文件:【标签】,实例属性,实例属性,硬盘路径

String   默认路径 =  Application.StartupPath+"\\Config.ini";

private string ConfigPath = Application.StartupPath + "\\Config.ini";

更新:

2024

原版: 

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

namespace help
{
   public  class Help_Ini
    {
        #region API函数声明 (应用编程接口)
        //using System.Runtime.InteropServices;
        [DllImport("kernel32")]//返回0表示失败,非0为成功 (比如调用C++ 非托管类型的DLL)
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        #endregion
        /// <summary>
        /// get读
        /// private string path = Application.StartupPath + "\\Config\\Port.ini";
        /// 路径如"C:\\Users\\Administrator\\Desktop\\ScannerProj\\ScannerProj\\ScannerProj\\bin\\Debug\\Config\\Port.ini"
        /// </summary>
        /// <param name="Section">标签</param>
        /// <param name="Key">属性</param>
        /// <param name="NoText">值</param>
        /// <param name="iniFilePath">硬盘路径</param>
        /// <returns>string</returns>
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {//using System.IO;
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// set写
        /// </summary>
        /// <param name="Section">标签</param>
        /// <param name="Key">属性</param>
        /// <param name="Value">值</param>
        /// <param name="iniFilePath">硬盘路径</param>
        /// <returns>bool</returns>
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                return WritePrivateProfileString(Section, Key, Value, iniFilePath) != 0;
            }
            return false;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IniHelperDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.Load += FrmMain_Load;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.txt_IP.Text = IniConfigHelper.ReadIniData("PLC参数", "IP", "", ConfigPath);
            this.txt_Port.Text = IniConfigHelper.ReadIniData("PLC参数", "Port", "", ConfigPath);
        }

        private string ConfigPath = Application.StartupPath + "\\Config.ini";
        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (!File.Exists(ConfigPath))
            {
                FileStream fs = new FileStream(ConfigPath, FileMode.Create);
                fs.Close();
            }

            bool Result = true;

            Result&= IniConfigHelper.WriteIniData("PLC参数", "IP", this.txt_IP.Text.Trim(), ConfigPath);

            Result &= IniConfigHelper.WriteIniData("PLC参数", "Port", this.txt_Port.Text.Trim(), ConfigPath);

            if (Result)
            {
                MessageBox.Show("保存成功", "保存参数");
            }
            else
            {
                MessageBox.Show("保存失败", "保存参数");
            }
        }

        private void Btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值