国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET下INI配置文件操作類

2024-07-10 12:58:56
字體:
來源:轉載
供稿:網友
 

using microsoft.visualbasic.compilerservices;
using system;
using system.collections;
using system.runtime.interopservices;
using system.text;

namespace inimanage
{
 /// <summary>
 /// inimanage 的摘要說明
 /// 在http://www.allapi.net/ 上發現了一個vb.net的ini文件操作類,下載了看了看,順手改成了c#版的
 /// 你可以把它編譯成dll在winform或webform中引用,也可以直接把代碼拷到項目中使用
 /// 我沒有進行逐項測試,所以可能有不對的地方,請酌情修改
 /// --------------叢興滋(cncxz) 2005-08-23
 /// </summary>
 public class inimanage
 {

  #region" 引入相關dll "

  [dllimport("kernel32.dll", entrypoint="getprivateprofileinta", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int getprivateprofileint(string lpapplicationname, string lpkeyname, int ndefault, string lpfilename);

  [dllimport("kernel32.dll", entrypoint="getprivateprofilesectionsnamesa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int getprivateprofilesectionsnames(byte[] lpszreturnbuffer, int nsize, string lpfilename);

  [dllimport("kernel32.dll", entrypoint="getprivateprofilestringa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int getprivateprofilestring(string lpapplicationname, string lpkeyname, string lpdefault, stringbuilder lpreturnedstring, int nsize, string lpfilename);

  [dllimport("kernel32.dll", entrypoint="getprivateprofilestructa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int getprivateprofilestruct(string lpszsections, string lpszkey, byte[] lpstruct, int usizestruct, string szfile);
 
  [dllimport("kernel32.dll", entrypoint="writeprivateprofilesectionsa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int writeprivateprofilesections(string lpappname, string lpstring, string lpfilename);

  [dllimport("kernel32.dll", entrypoint="writeprivateprofilestringa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int writeprivateprofilestring(string lpapplicationname, string lpkeyname, string lpstring, string lpfilename);

  [dllimport("kernel32.dll", entrypoint="writeprivateprofilestructa", callingconvention=callingconvention.stdcall, charset=charset.ansi, exactspelling=true)]
  private static extern int writeprivateprofilestruct(string lpszsections, string lpszkey, byte[] lpstruct, int usizestruct, string szfile);

  #endregion


  private string _filename;    //ini文件名
  private string _sections;     //ini文件中配置參數的組別片段
  private const int max_entry = 32768; //最大字符數


  public inimanage(string strfile)
  {
   this.filename = strfile;
  }

  #region" ini操作類的屬性 "

  public string filename
  {
   get
   {
    return this._filename;
   }
   set
   {
    this._filename = value;
   }
  }


  public string sections
  {
   get
   {
    return this._sections;
   }
   set
   {
    this._sections = value;
   }
  }


  #endregion


  #region" ini操作類的read相關方法 "
 
  // read相關方法中的 defaultvalue是 在ini文件中找不到相關配置 時的返回值
  //readboolean是讀取bool類型的配置參數,readbytearray是讀取 byte[]類型的配置參數
  //readinteger是讀取int類型的配置參數。。。。依次類推

  public bool readboolean(string key)
  {
   return this.readboolean(this.sections, key);
  }

  public bool readboolean(string key, bool defaultvalue)
  {
   return this.readboolean(this.sections, key, defaultvalue);
  }

  public bool readboolean(string sections, string key)
  {
   return this.readboolean(sections, key, false);
  }

  public bool readboolean(string sections, string key, bool defaultvalue)
  {
   return bool.parse(this.readstring(sections, key, defaultvalue.tostring()));
  }

  public byte[] readbytearray(string key, int length)
  {
   return this.readbytearray(this.sections, key, length);
  }

  public byte[] readbytearray(string sections, string key, int length)
  {
   byte[] buffer1;
   if (length > 0)
   {
    try
    {
     byte[] buffer2 = new byte[(length - 1) + 1];
     if (inimanage.getprivateprofilestruct(sections, key, buffer2, buffer2.length, this.filename) == 0)
     {
      return null;
     }
     return buffer2;
    }
    catch (exception exception1)
    {
     projectdata.setprojecterror(exception1);
     buffer1 = null;
     projectdata.clearprojecterror();
     return buffer1;               
    }
   }
   else
   {
    return null;
   }
       
  }


  public int readinteger(string key)
  {
   return this.readinteger(key, 0);
  }

  public int readinteger(string key, int defaultvalue)
  {
   return this.readinteger(this.sections, key, defaultvalue);
  }

  public int readinteger(string sections, string key)
  {
   return this.readinteger(sections, key, 0);
  }

  public int readinteger(string sections, string key, int defaultvalue)
  {
   int num1;
   try
   {
    num1 = inimanage.getprivateprofileint(sections, key, defaultvalue, this.filename);
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    num1 = defaultvalue;
    projectdata.clearprojecterror();
    return num1;           
   }
   return num1;
  }

  public long readlong(string key)
  {
   return this.readlong(key, (long) 0);
  }

  public long readlong(string key, long defaultvalue)
  {
   return this.readlong(this.sections, key, defaultvalue);
  }

  public long readlong(string sections, string key)
  {
   return this.readlong(sections, key, 0);
  }

  public long readlong(string sections, string key, long defaultvalue)
  {
   return long.parse(this.readstring(sections, key, defaultvalue.tostring()));
  }

  public string readstring(string key)
  {
   return this.readstring(this.sections, key);
  }

  public string readstring(string sections, string key)
  {
   return this.readstring(sections, key, "");
  }

  public string readstring(string sections, string key, string defaultvalue)
  {
   string text1;
   try
   {
    stringbuilder builder1 = new stringbuilder(max_entry);
    int num1 = inimanage.getprivateprofilestring(sections, key, defaultvalue, builder1, max_entry, this.filename);
    text1 = builder1.tostring();
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    text1 = defaultvalue;
    projectdata.clearprojecterror();
    return text1;
         
   }
   return text1;
  }

  #endregion


  #region" ini操作類的write相關方法 "
 
  public bool write(string key, bool value)
  {
   return this.write(this.sections, key, value);
  }

  public bool write(string key, byte[] value)
  {
   return this.write(this.sections, key, value);
  }

  public bool write(string key, string value)
  {
   return this.write(this.sections, key, value);
  }

  public bool write(string key, int value)
  {
   return this.write(this.sections, key, value);
  }

  public bool write(string key, long value)
  {
   return this.write(this.sections, key, value);
  }

  public bool write(string sections, string key, byte[] value)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilestruct(sections, key, value, value.length, this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;          
   }
   return flag1;
  }

  public bool write(string sections, string key, bool value)
  {
   return this.write(sections, key, value.tostring());
  }

  public bool write(string sections, string key, int value)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilestring(sections, key, value.tostring(), this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;          
   }
   return flag1;
  }

  public bool write(string sections, string key, long value)
  {
   return this.write(sections, key, value.tostring());
  }

  public bool write(string sections, string key, string value)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilestring(sections, key, value, this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;          
   }
   return flag1;
  }

  #endregion


  #region" ini操作類的delete相關方法 "

  public bool deletekey(string key)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilestring(this.sections, key, null, this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;           
   }
   return flag1;
  }

  public bool deletekey(string section, string key)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilestring(sections, key, null, this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;          
   }
   return flag1;
  }

  public bool deletesections(string section)
  {
   bool flag1;
   try
   {
    flag1 = inimanage.writeprivateprofilesections(sections, null, this.filename) != 0;
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    flag1 = false;
    projectdata.clearprojecterror();
    return flag1;          
   }
   return flag1;
  }

  #endregion

  public arraylist getsectionsnames()
  {
   int num1;
   arraylist list1 = new arraylist();
   byte[] buffer1 = new byte[max_entry];
   int num2 = 0;
   try
   {
    num1 = inimanage.getprivateprofilesectionsnames(buffer1, max_entry, this.filename);
   }
   catch (exception exception1)
   {
    projectdata.setprojecterror(exception1);
    projectdata.clearprojecterror();
    return list1;         
   }
   asciiencoding encoding1 = new asciiencoding();
   if (num1 > 0)
   {
    string text1 = encoding1.getstring(buffer1);
    num1 = 0;
    num2 = -1;
    while (true)
    {
     num1 = text1.indexof('/0', (int) (num2 + 1));
     if (((num1 - num2) == 1) || (num1 == -1))
     {
      return list1;
     }
     try
     {
      list1.add(text1.substring(num2 + 1, num1 - num2));
     }
     catch (exception exception2)
     {
      projectdata.setprojecterror(exception2);
      projectdata.clearprojecterror();
     }
     num2 = num1;
    }
   }
   return list1;
  }

 }
}


  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 班戈县| 禄丰县| 巴东县| 探索| 丽江市| 宁南县| 乐至县| 钟祥市| 特克斯县| 邹城市| 南靖县| 和静县| 永胜县| 静安区| 盖州市| 衡山县| 常山县| 肇州县| 房产| 清新县| 汉沽区| 绥阳县| 东宁县| 获嘉县| 新干县| 乐山市| 湟中县| 伊宁市| 灵丘县| 淮北市| 井冈山市| 昭觉县| 纳雍县| 肇庆市| 莱阳市| 鄄城县| 疏勒县| 四子王旗| 册亨县| 永寿县| 房产|