你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

asp.net 操作INI文件的读写,读写操作本地ini配置文件

2021/12/28 12:12:25

INIFile 方法的定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;

namespace WebApplication9
{
    public class INIFile
    {
        public string path;

        public INIFile(string INIPath)
        {
            path = INIPath;
        }

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

        [DllImport("kernel32")]

        private static extern int GetPrivateProfileString(string section,
         string key, string def, StringBuilder retVal, int size, string filePath);

        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }


        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);

            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);

            return temp.ToString();

        }
    }
}

ini文件写入

INIFile ab = new INIFile(@"D:\project\WebApplication9\Token");
                string iniFile = @"D:\project\WebApplication9\Token\Token.ini";
                if (!File.Exists(iniFile))
                {
                    using (FileStream fs = File.Create(iniFile))
                    {
                        fs.Close();
                    }
                }

                INIFile myINI = new INIFile(iniFile);

                myINI.IniWriteValue("User", T_USER .Text, T_PW.Text);

ini文件读取

INIFile ab = new INIFile(@"D:\project\WebApplication9\Token\Token.ini");
               pw = ab.IniReadValue("User", T_USER.Text );

ini文件