• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#读取和配置IniFile

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

自定义IniFile操作类

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

namespace Sisley
{
    public  class INIFileHepler
    {
       
        private string  Default_IniFileName = "Config.ini"; //INI文件名
        private string strIniFilePath;

        // 返回0表示失败,非0为成功  
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 返回取得字符串缓冲区的长度  
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);
        /*
         [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
         public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);  
        */

        public INIFileHepler()  
        {
            this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" +Default_IniFileName; 

        }  

 public INIFileHepler(string strIniFilePath)
        {
            if  ( Directory.Exists(strIniFilePath))
            {
                this.strIniFilePath = Directory.GetCurrentDirectory() + "\\" + Default_IniFileName;

            }

  this.strIniFilePath = strIniFilePath;
        }
       
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="section">写入section</param>
        /// <param name="key">写入key</param>
        /// <param name="value">写入value</param>
        public void  WriteIniString(string section, string key, string value)  
        {
            try
            {
                WritePrivateProfileString(section, key, value, strIniFilePath);
            }
            catch
            {
                throw new Exception("配置文件不存在或权限不足导致无法写入");
            }
          
        }

        /// <summary>  
        /// 获取ini配置文件中的字符串  
        /// </summary>  
        /// <param name="section">节名</param>  
        /// <param name="key">键名</param>  
        /// <param name="strDefault">默认值</param>  
        /// <param name="retVal">结果缓冲区</param>  
        /// <param name="size">结果缓冲区大小</param>  
        /// <returns>返回值</returns>  
        public string  GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
        {
            long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
            //return (liRet >= 1);

            StringBuilder temp = new StringBuilder(1024);
            INIFileHepler.GetPrivateProfileString(section, key, strDefault, temp, 1024, strIniFilePath);
            return temp.ToString();
        }  


    }
}
View Code

使用示例:

设置和设置

  INIFileHepler inifile = new INIFileHepler();
        //INIFileHepler inifile = new INIFileHepler(@"D:\MyDocs\CSharp\Program\bin\Debug\TestConfig.ini");
        private void button1_Click(object sender, EventArgs e)
        {
                    
            inifile.WriteIniString("DatabaseConfig", "Server", "192.168.10.3");
            inifile.WriteIniString("DatabaseConfig", "DBName", "DB_Live");
            inifile.WriteIniString("DatabaseConfig", "UserName", "admin");
            inifile.WriteIniString("DatabaseConfig", "UserPassword", "123456");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            richTextBox1.AppendText("DatabaseConfig"+"\n");
            StringBuilder Server = new StringBuilder(32);
            StringBuilder DBName = new StringBuilder(32);
            StringBuilder UserName = new StringBuilder(32);
            StringBuilder UserPassword = new StringBuilder(32);

            inifile.GetIniString("DatabaseConfig", "Server", "", Server, Server.Capacity);

            inifile.GetIniString("DatabaseConfig", "DBName", "", DBName, DBName.Capacity);
            inifile.GetIniString("DatabaseConfig", "UserName", "", UserName, UserName.Capacity);
            inifile.GetIniString("DatabaseConfig", "UserPassword", "", UserPassword, UserPassword.Capacity);
            
            richTextBox1.AppendText("Server:   " +Server+  "\n");
            richTextBox1.AppendText("DBName:   " + DBName + "\n");
            richTextBox1.AppendText("UserName:   " + UserName + "\n");
            richTextBox1.AppendText("UserPassword:   " + UserPassword + "\n");
   }
View Code

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#中的类型转换发布时间:2022-07-10
下一篇:
C#多线程开发:并行、并发与异步编程发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap