zoukankan      html  css  js  c++  java
  • C# INI文件操作类(IniFileAPI)

    C# Code:

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

    namespace CSFramework.Tech2011
    {
       public class IniFileAPI
       {
          private string fileName;
          [DllImport("kernel32")]
          private static extern int GetPrivateProfileInt(
          string lpAppName,// 指向包含 Section 名称的字符串地址
          string lpKeyName,// 指向包含 Key 名称的字符串地址
          int nDefault,// 如果 Key 值没有找到,则返回缺省的值是多少
          string lpFileName
          );
          [DllImport("kernel32")]
          private static extern int GetPrivateProfileString(
          string lpAppName,// 指向包含 Section 名称的字符串地址
          string lpKeyName,// 指向包含 Key 名称的字符串地址
          string lpDefault,// 如果 Key 值没有找到,则返回缺省的字符串的地址
          StringBuilder lpReturnedString,// 返回字符串的缓冲区地址
          int nSize,// 缓冲区的长度
          string lpFileName
          );
          [DllImport("kernel32")]
          private static extern bool WritePrivateProfileString(
          string lpAppName,// 指向包含 Section 名称的字符串地址
          string lpKeyName,// 指向包含 Key 名称的字符串地址
          string lpString,// 要写的字符串地址
          string lpFileName
          );
         
          public void IniFile(string filename)
          {
             fileName = filename;
          }
          public int GetInt(string section, string key, int def)
          {
             return GetPrivateProfileInt(section, key, def, fileName);
          }
          public string GetString(string section, string key, string def)
          {
             StringBuilder temp = new StringBuilder(1024);
             GetPrivateProfileString(section, key, def, temp, 1024, fileName);
             return temp.ToString();
          }
          public void WriteInt(string section, string key, int iVal)
          {
             WritePrivateProfileString(section, key, iVal.ToString(), fileName);
          }
          public void WriteString(string section, string key, string strVal)
          {
             WritePrivateProfileString(section, key, strVal, fileName);
          }
          public void DelKey(string section, string key)
          {
             WritePrivateProfileString(section, key, null, fileName);
          }
          public void DelSection(string section)
          {
             WritePrivateProfileString(section, null, null, fileName);
          }
         
       }
    }

  • 相关阅读:
    如何在文本编辑器中实现搜索功能? 字符串比较算法 BF算法 RK算法
    怎么读源码 读源码的一些技巧
    系统性学习
    堆 二叉堆 找流的中位数
    apk系统签名小技巧
    常用adb命令总结
    Android6.0 源码修改之Setting列表配置项动态添加和静态添加
    AndroidStudio开发Java工程(解决java控制台中文打印乱码+导入jar包运行工程)
    加载loading对话框的功能(不退出沉浸式效果)
    Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
  • 原文地址:https://www.cnblogs.com/lvfeilong/p/hshgfgfh.html
Copyright © 2011-2022 走看看