C# Read/Write multiple line from .ini

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Len
    New Member
    • Oct 2010
    • 13

    C# Read/Write multiple line from .ini

    I currently working on a C# application where need to store information into .ini files.

    I perform some search online and able to read and write info to .ini by importing kernel32 to my application.

    But now i come across problem where i want to insert and read multiple record using loop, because when I add another record, it just replace the previous record.

    My first question is how to make it insert 3 record to the .ini eg:
    [Username]
    Name=abc
    [Username]
    Name=bcd
    [Username]
    Name=cde

    Or hopefully can be like this

    [Username]
    Name=abc
    Name=bcd
    Name=cde

    Also i would like to read these record using loop and assign the Name into an array.

    Any suggestion?
  • Alex Bug
    New Member
    • Oct 2010
    • 3

    #2
    You can try to use function WritePrivatePro fileString.
    MSDN Library
    I would use XML.

    Comment

    • Subin Ninan
      New Member
      • Sep 2010
      • 91

      #3
      You can use xml file to read & write data without importing unmanaged code.

      Comment

      • Sean Len
        New Member
        • Oct 2010
        • 13

        #4
        Thanks, I will take a look into XML.

        Comment

        • shedeep

          #5
          Thanks, I will take a look into XML

          Comment

          • Sean Len
            New Member
            • Oct 2010
            • 13

            #6
            Hello guys, I research in XML and able to perform write to XML. But same problem occur. Here is my code, can any please tell me why new record will only replace the previous record?

            private void btnStore_Click( object sender, EventArgs e)
            {
            XmlTextWriter textWriter = new XmlTextWriter(" C:\\test.xml", null);

            textWriter.Writ eStartDocument( );

            textWriter.Writ eStartElement(" User");

            textWriter.Writ eStartElement(" Name", "");
            textWriter.Writ eString(name.Te xt);
            textWriter.Writ eEndElement();

            textWriter.Writ eStartElement(" Age", "");
            textWriter.Writ eString(age.Tex t);
            textWriter.Writ eEndElement();

            textWriter.Writ eEndDocument();

            textWriter.Clos e();
            }

            The XML Generated:
            <?xml version="1.0" ?>
            - <User>
            <Name>User Name1</Name>
            <Age>Age1</Age>
            </User>

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              I don't understand the problem. That output looks like it follows from the code you posted (please use code tags next time!!).

              If you wanted another user within the User node, you'd just write another one. Can you please clarify the problem?

              Hmm, also, may I point out an alternative method of generating XML? I'm not saying your way isn't valid, I just find it a bit easier to understand so I'll show you an example and you can decide.

              Code:
                          XmlDocument xmlDoc = new XmlDocument();
                          XmlNode usersNode = xmlDoc.CreateNode(XmlNodeType.Element, "Users", "");
                          xmlDoc.AppendChild(usersNode);
              
                          XmlNode user1 = xmlDoc.CreateNode(XmlNodeType.Element, "User", "");
                          XmlAttribute user1NameAttrib = xmlDoc.CreateAttribute("Name");
                          user1NameAttrib.Value = "Gary Texmo";
              
                          XmlAttribute user1AgeAttrib = xmlDoc.CreateAttribute("Age");
                          user1AgeAttrib.Value = "28";
              
                          user1.Attributes.Append(user1NameAttrib);
                          user1.Attributes.Append(user1AgeAttrib);
              
                          usersNode.AppendChild(user1);
              
                          XmlNode user2 = xmlDoc.CreateNode(XmlNodeType.Element, "User", "");
                          XmlAttribute user2NameAttrib = xmlDoc.CreateAttribute("Name");
                          user2NameAttrib.Value = "John Smith";
              
                          XmlAttribute user2AgeAttrib = xmlDoc.CreateAttribute("Age");
                          user2AgeAttrib.Value = "45";
              
                          user2.Attributes.Append(user2NameAttrib);
                          user2.Attributes.Append(user2AgeAttrib);
              
                          usersNode.AppendChild(user2);
              
                          Console.WriteLine(xmlDoc.OuterXml);

              Comment

              • Sean Len
                New Member
                • Oct 2010
                • 13

                #8
                Thanks gary. You way works without the problem i facing!
                In my code there, when i insert one record no problem.
                But when i want to add another record, it just replace the previous one.
                After all i found an alternative which use append.
                Just try out ur solution and it work just perfect!
                Thanks alot ya!

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  I'm not sure why, but there seems to be like 800 different ways to work with XML in C# (gross exaggeration, but some days I feel like it's true!) so it's easy to be confused. Personally, I like to work with the XmlNode object and associated classes.

                  Glad it's working for you.

                  Comment

                  Working...