Home>

Although Microsoft has already suggested using the registry to replace the ini file in windows, in actual applications,ini files are still useful,Especially now the popularity of green software,More and more programs save some of their configuration information in ini files.

ini files are text files,Consists of sections, under each bracketed title,Is a number of keywords and their corresponding values

[section]

key=value

vc provides API functions to read and write ini files,However, there is no corresponding method in the c#programming language introduced by Microsoft.The following is a c#ini file read and write class,Collected from the internet,It is complete, that is, there is no renaming function of section.Master can add one.

using system;
using system.io;
using system.runtime.interopservices;
using system.text;
using system.collections;
using system.collections.specialized;
namespace wuyisky
{
 ///<summary>
 ///classes of inifiles
 ///</summary>
 public class inifiles
 {
  public string filename;//ini file name
  //Declaring API functions for reading and writing ini files
  [dllimport ("kernel32")]
  private static extern bool writeprivateprofilestring (string section, string key, string val, string filepath);
  [dllimport ("kernel32")]
  private static extern int getprivateprofilestring (string section, string key, string def, byte [] retval, int size, string filepath);
  //The constructor of the class,Pass ini file name
  public inifiles (string afilename)
  {
   //determine if the file exists
   fileinfo fileinfo=new fileinfo (afilename);
   //todo:figure out how to use enums
   if ((! fileinfo.exists))
   {//|| (fileattributes.directory in fileinfo.attributes))
    //file does not exist,Create document
    system.io.streamwriter sw=new system.io.streamwriter (afilename, false, system.text.encoding.default);
    try
    {
     sw.write ("#FORM Configuration File");
     sw.close ();
    }
    catch
    {
     throw (new applicationexception ("ini file does not exist"));
    }
   }
   //must be a full path,Cannot be a relative path
   filename=fileinfo.fullname;
  }
  //Write ini file
  public void writestring (string section, string ident, string value)
  {
   if (! writeprivateprofilestring (section, ident, value, filename))
   {
    throw (new applicationexception ("Error writing ini file"));
   }
  }
  //Read the ini file specified
  public string readstring (string section, string ident, string default)
  {
   byte [] buffer=new byte [65535];
   int buflen=getprivateprofilestring (section, ident, default, buffer, buffer.getupperbound (0), filename);
   //The encoding method of 0 (the default code page of the system) must be set,Otherwise it cannot support Chinese
   string s=encoding.getencoding (0) .getstring (buffer);
   s=s.substring (0, buflen);
   return s.trim ();
  }
  //Read integer
  public int readinteger (string section, string ident, int default)
  {
   string intstr=readstring (section, ident, convert.tostring (default));
   try
   {
    return convert.toint32 (intstr);
   }
   catch (exception ex)
   {
    console.writeline (ex.message);
    return default;
   }
  }
  //write integer
  public void writeinteger (string section, string ident, int value)
  {
   writestring (section, ident, value.tostring ());
  }
  //read boolean
  public bool readbool (string section, string ident, bool default)
  {
   try
   {
    return convert.toboolean (readstring (section, ident, convert.tostring (default)));
   }
   catch (exception ex)
   {
    console.writeline (ex.message);
    return default;
   }
  }
  //write bool
  public void writebool (string section, string ident, bool value)
  {
   writestring (section, ident, convert.tostring (value));
  }
  //From the ini file, add all idents in the specified section name to the list
  public void readsection (string section, stringcollection idents)
  {
   byte [] buffer=new byte [16384];
   //idents.clear ();
   int buflen=getprivateprofilestring (section, null, null, buffer, buffer.getupperbound (0),     filename);
   //Parse the section
   getstringsfrombuffer (buffer, buflen, idents);
  }
  private void getstringsfrombuffer (byte [] buffer, int buflen, stringcollection strings)
  {
   strings.clear ();
   if (buflen!=0)
   {
    int start=0;
    for (int i=0;i<buflen;i ++)
    {
     if ((buffer [i] == 0)&&((i-start)>0))
     {
      string s=encoding.getencoding (0) .getstring (buffer, start, i-start);
      strings.add (s);
      start=i + 1;
     }
    }
   }
  }
  //Read the names of all sections from the ini file
  public void readsections (stringcollection sectionlist)
  {
   //note:must be implemented in bytes, stringbuilder can only take the first section
   byte [] buffer=new byte [65535];
   int buflen=0;
   buflen=getprivateprofilestring (null, null, null, buffer,    buffer.getupperbound (0), filename);
   getstringsfrombuffer (buffer, buflen, sectionlist);
  }
  //Read all values ​​of the specified section into the list
  public void readsectionvalues ​​(string section, namevaluecollection values)
  {
   stringcollection keylist=new stringcollection ();
   readsection (section, keylist);
   values.clear ();
   foreach (string key in keylist)
   {
    values.add (key, readstring (section, key, ""));
   }
  }
  ////Read all values ​​of the specified section into the list,  //public void readsectionvalues ​​(string section, namevaluecollection values, char splitstring)
  //{string sectionvalue;
  //string [] sectionvaluesplit;
  //stringcollection keylist=new stringcollection ();
  //readsection (section, keylist);
  //values.clear ();
  //foreach (string key in keylist)
  //{
  //sectionvalue=readstring (section, key, "");
  //sectionvaluesplit=sectionvalue.split (splitstring);
  //values.add (key, sectionvaluesplit [0] .tostring (), sectionvaluesplit [1] .tostring ());
  //}
  //}
  //Clear a section
  public void erasesection (string section)
  {
   if (! writeprivateprofilestring (section, null, null, filename))
   {
    throw (new applicationexception ("Cannot clear section in ini file"));
   }
  }
  //Delete the key under a section
  public void deletekey (string section, string ident)
  {
   writeprivateprofilestring (section, ident, null, filename);
  }
  //note:For win9x, you need to implement the updatefile method to write the data in the buffer to a file
  //On win nt, 2000 and xp, write files directly,Without buffering,So no need to implement updatefile
  //After executing the modification to the ini file,This method should be called to update the buffer.
  public void updatefile ()
  {
   writeprivateprofilestring (null, null, null, filename);
  }
  //Check if a key value under a section exists
  public bool valueexists (string section, string ident)
  {
   stringcollection idents=new stringcollection ();
   readsection (section, idents);
   return idents.indexof (ident)>-1;
  }
  //Ensure the release of resources
  ~ inifiles ()
  {
   updatefile ();
  }
 }
}

At present, c#operations on ini files are basically replaced by xml files.But I think the reading and writing of ini files is still the basic of programming,Must have

c
  • Previous aspnet paging function implementation
  • Next JavaMe development draw text box TextEdit