473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
Oct 5 '10 #1
8 5453
You can try to use function WritePrivateProfileString.
MSDN Library
I would use XML.
Oct 5 '10 #2
You can use xml file to read & write data without importing unmanaged code.
Oct 5 '10 #3
Thanks, I will take a look into XML.
Oct 5 '10 #4
Thanks, I will take a look into XML
Oct 5 '10 #5
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.WriteStartDocument();

textWriter.WriteStartElement("User");

textWriter.WriteStartElement("Name", "");
textWriter.WriteString(name.Text);
textWriter.WriteEndElement();

textWriter.WriteStartElement("Age", "");
textWriter.WriteString(age.Text);
textWriter.WriteEndElement();

textWriter.WriteEndDocument();

textWriter.Close();
}

The XML Generated:
<?xml version="1.0" ?>
- <User>
<Name>User Name1</Name>
<Age>Age1</Age>
</User>
Oct 6 '10 #6
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1.             XmlDocument xmlDoc = new XmlDocument();
  2.             XmlNode usersNode = xmlDoc.CreateNode(XmlNodeType.Element, "Users", "");
  3.             xmlDoc.AppendChild(usersNode);
  4.  
  5.             XmlNode user1 = xmlDoc.CreateNode(XmlNodeType.Element, "User", "");
  6.             XmlAttribute user1NameAttrib = xmlDoc.CreateAttribute("Name");
  7.             user1NameAttrib.Value = "Gary Texmo";
  8.  
  9.             XmlAttribute user1AgeAttrib = xmlDoc.CreateAttribute("Age");
  10.             user1AgeAttrib.Value = "28";
  11.  
  12.             user1.Attributes.Append(user1NameAttrib);
  13.             user1.Attributes.Append(user1AgeAttrib);
  14.  
  15.             usersNode.AppendChild(user1);
  16.  
  17.             XmlNode user2 = xmlDoc.CreateNode(XmlNodeType.Element, "User", "");
  18.             XmlAttribute user2NameAttrib = xmlDoc.CreateAttribute("Name");
  19.             user2NameAttrib.Value = "John Smith";
  20.  
  21.             XmlAttribute user2AgeAttrib = xmlDoc.CreateAttribute("Age");
  22.             user2AgeAttrib.Value = "45";
  23.  
  24.             user2.Attributes.Append(user2NameAttrib);
  25.             user2.Attributes.Append(user2AgeAttrib);
  26.  
  27.             usersNode.AppendChild(user2);
  28.  
  29.             Console.WriteLine(xmlDoc.OuterXml);
Oct 6 '10 #7
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!
Oct 6 '10 #8
GaryTexmo
1,501 Expert 1GB
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.
Oct 6 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw,...
8
by: MrTrix | last post by:
Hello: I'm having a problem formulating the code to execute a multiple line command. I'm trying to execute something like: set rowcount 100000 declare @rowct int select @rowct = 1 while...
2
by: Wow | last post by:
Do I have to replace all new lines with \n and document.write one line? Is there something like perl's print lines print << 'body'; body in javascripts?
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
2
by: Drew | last post by:
Ok, I'm trying to do the simplest read/write from one csv file to another. For some reason, every other row on the output file is a blank row. What am I doing wrong? import csv reader =...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
6
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I...
3
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? ...
4
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.