Skip to content

Reading INI Files

mr_tolil

Asked Jan 28, 2004, 6:00 PM EST4 answers
Share
LinkedInX
Can anyone help me how to read an .INI Files? is there any special function to do it? as far as I know at VB6, there is an API function to read this File (.INI). Regars, Mr. TOLIL (Newbie in .NET)

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    you can use the same api's in VB.NET .NET tries to keep you away from INI files thats why there is no built in calls for INI, if you are creating them in your ur own app, u are better off using XML files try my 2 functions i wrote string getXML = getXMLNodeFromXMLFile(GlobalStuff.SettingsFile, "SearchSettings", "Code")) [code] public static string getXMLNodeFromXMLFile( string xmlFile, string parentNode, string node) { try { XmlDocument doc = new XmlDocument(); // get an XML document doc.Load(xmlFile); //load the file XmlNodeList xmlNode = doc.GetElementsByTagName(node); for(int i = 0; i < xmlNode.Count; i++) //loop through all the found nodes { if (xmlNode[i].ParentNode.Name == parentNode) //find the one we want in the collection { return xmlNode[i].InnerText.ToString(); //return the text } } return "Node belonging to " + parentNode + " was not found in " + xmlFile; } catch(System.Exception e) //catch the exception such as if the file passed in is not found { return e.Message; } } [/code] setXMLNodeInXMLFile(GlobalStuff.SettingsFile, "SearchSettings", "Code", "ON"); [code] public static bool setXMLNodeInXMLFile( string xmlFile, string parentNode, string node, string nodeValue) { try { XmlDocument doc = new XmlDocument(); // get an XML document doc.Load(xmlFile); //load the file XmlNodeList xmlNode = doc.GetElementsByTagName(node); for(int i = 0; i < xmlNode.Count; i++) //loop through all the found nodes { if (xmlNode[i].ParentNode.Name == parentNode) //find the one we want in the collection { xmlNode[i].InnerText = nodeValue; //set the text doc.Save(xmlFile); //save the file back return true; } } return false; } catch(System.Exception) //catch the exception such as if the file passed in is not found { return false; } } [/code]
    0
  • 0
    For a VB .NET user unfamiliar with C that probably looks like hieroglyphics.
    0
  • 0
    lol, most definetely does.... To solve the INI problem (which microsoft is reportedly going back to in longhorn to avoid users fux0ring their registry's) I would build a class INI. I am doing something similar in a card game that keeps track of all the highest scores in a poker game. After you have built a class of INI, define the properties you want to save and serialize them before the application quits (at form close or something like that). When your program restarts, you would have to deserialize the info from the INI and use the property SET u defined when you made the the property. Remember, .INI works like a sequential access file! This is extremely important when serializing and deserializing. Hope I've helped!
    0
  • 0
    You can use this library to read your ini files with VB.NET. It's quite straightforward, when reading it you just iterate through ini file's sections and keys of each section.

    You can also use the approach mentioned by URIscorey, for example here is how you can read and deserialize the ini file's content:
    VB.NET
    ' Load INI file.
    Dim ini As New IniFile()
    ini.Load("your-ini-file.ini")
    
    ' Create your object from INI file's content.
    Dim mySettingsObject = ini.Sections(0).Deserialize(Of CustomClass)()
    Also here is how you can serialize and save that content:
    Code
    ' Remove previous settings.
    ini.Sections(0).Keys.Clear()
    ' Write current settings.
    ini.Sections(0).Serialize(mySettingsObject)
    ' Save INI file.
    ini.Save("your-ini-file.ini")
    0