How to read text file based on key values


hi,

i have text file ini file, how read text file based on keys

i need values after "="  symbol

here example lines.

lbldays1=days
lblinhex=in hexadecimal notation
information=information
lblhjunk=junk e-mail folder excluded selected mailboxes
lblhfqdn=fully qualified domain name. find fqdn, open exchange system manager, expand recipeints, open recipient policies, double click on default policy, in properties windows, navigate email addresses(policy) tab, find out default smtp entry(the highlighted one), fqdn(without first @ symbol)
lblftypeincluded=file types included
lbl60000=(60-6000)
lblexchusername=username
lbl10000=(1-10000)

hi,

you use code above. 

using system; using system.io; using system.collections;  public class iniparser {     private hashtable keypairs = new hashtable();     private string inifilepath;      private struct sectionpair     {         public string section;         public string key;     }      /// <summary>     /// opens ini file @ given path , enumerates values in iniparser.     /// </summary>     /// <param name="inipath">full path ini file.</param>     public iniparser(string inipath)     {         textreader inifile = null;         string strline = null;         string currentroot = null;         string[] keypair = null;          inifilepath = inipath;          if (file.exists(inipath))         {             try             {                 inifile = new streamreader(inipath);                  strline = inifile.readline();                  while (strline != null)                 {                     strline = strline.trim().toupper();                      if (strline != "")                     {                         if (strline.startswith("[") && strline.endswith("]"))                         {                             currentroot = strline.substring(1, strline.length - 2);                         }                         else                         {                             keypair = strline.split(new char[] { '=' }, 2);                              sectionpair sectionpair;                             string value = null;                              if (currentroot == null)                                 currentroot = "root";                              sectionpair.section = currentroot;                             sectionpair.key = keypair[0];                              if (keypair.length > 1)                                 value = keypair[1];                              keypairs.add(sectionpair, value);                         }                     }                      strline = inifile.readline();                 }              }             catch (exception ex)             {                 throw ex;             }                         {                 if (inifile != null)                     inifile.close();             }         }         else             throw new filenotfoundexception("unable locate " + inipath);      }      /// <summary>     /// returns value given section, key pair.     /// </summary>     /// <param name="sectionname">section name.</param>     /// <param name="settingname">key name.</param>     public string getsetting(string sectionname, string settingname)     {         sectionpair sectionpair;         sectionpair.section = sectionname.toupper();         sectionpair.key = settingname.toupper();          return (string)keypairs[sectionpair];     }      /// <summary>     /// enumerates lines given section.     /// </summary>     /// <param name="sectionname">section enum.</param>     public string[] enumsection(string sectionname)     {         arraylist tmparray = new arraylist();          foreach (sectionpair pair in keypairs.keys)         {             if (pair.section == sectionname.toupper())                 tmparray.add(pair.key);         }          return (string[])tmparray.toarray(typeof(string));     }      /// <summary>     /// adds or replaces setting table saved.     /// </summary>     /// <param name="sectionname">section add under.</param>     /// <param name="settingname">key name add.</param>     /// <param name="settingvalue">value of key.</param>     public void addsetting(string sectionname, string settingname, string settingvalue)     {         sectionpair sectionpair;         sectionpair.section = sectionname.toupper();         sectionpair.key = settingname.toupper();          if (keypairs.containskey(sectionpair))             keypairs.remove(sectionpair);          keypairs.add(sectionpair, settingvalue);     }      /// <summary>     /// adds or replaces setting table saved null value.     /// </summary>     /// <param name="sectionname">section add under.</param>     /// <param name="settingname">key name add.</param>     public void addsetting(string sectionname, string settingname)     {         addsetting(sectionname, settingname, null);     }      /// <summary>     /// remove setting.     /// </summary>     /// <param name="sectionname">section add under.</param>     /// <param name="settingname">key name add.</param>     public void deletesetting(string sectionname, string settingname)     {         sectionpair sectionpair;         sectionpair.section = sectionname.toupper();         sectionpair.key = settingname.toupper();          if (keypairs.containskey(sectionpair))             keypairs.remove(sectionpair);     }      /// <summary>     /// save settings new file.     /// </summary>     /// <param name="newfilepath">new file path.</param>     public void savesettings(string newfilepath)     {         arraylist sections = new arraylist();         string tmpvalue = "";         string strtosave = "";          foreach (sectionpair sectionpair in keypairs.keys)         {             if (!sections.contains(sectionpair.section))                 sections.add(sectionpair.section);         }          foreach (string section in sections)         {             strtosave += ("[" + section + "]\r\n");              foreach (sectionpair sectionpair in keypairs.keys)             {                 if (sectionpair.section == section)                 {                     tmpvalue = (string)keypairs[sectionpair];                      if (tmpvalue != null)                         tmpvalue = "=" + tmpvalue;                      strtosave += (sectionpair.key + tmpvalue + "\r\n");                 }             }              strtosave += "\r\n";         }          try         {             textwriter tw = new streamwriter(newfilepath);             tw.write(strtosave);             tw.close();         }         catch (exception ex)         {             throw ex;         }     }      /// <summary>     /// save settings ini file.     /// </summary>     public void savesettings()     {         savesettings(inifilepath);     } }

this implementation of class above:

public class testparser {     public static void main()     {         iniparser parser = new iniparser(@"c:\test.ini");          string newmessage;          newmessage = parser.getsetting("appsettings", "msgpart1");         newmessage += parser.getsetting("appsettings", "msgpart2");         newmessage += parser.getsetting("punctuation", "ex");          //returns "hello world!"         console.writeline(newmessage);         console.readline();     } }



joão sousa (mcts) senior software engineer



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'