Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and propertiy file

Status
Not open for further replies.

florins12

Programmer
Feb 27, 2004
2
RO
Hi,
I am new to C#. I'm making a project which requires a property file like this:
key1=value1
key2=value2
How can I read/modify this file in C# ? There is a class in C# like Properties in java ?

Thanks,
Florin
 
Java has Hash tables as does C#. You could explore that.
Marty
 
I know I could parse myself, but I'm thinking that maybe there is a class in .NET as in Java (java.util.Properties) which does this thing ...
 
There are many solutions.
Hers is one example:
Code:
Hashtable ht = new Hashtable();
open the file
Foreach line read from the file:
 string [] kv= line.split('=');
 ht.Add(kv[0],kv[1]);
close the file.
Now you can iterate the Hashtable, modify it:
if (ht.Containkey("key1")
    ht["key1"]="blabla";
When all changes are done the save the Hashtable:
open the output file
foreach (object o in ht.Keys)
{
  string line = o.ToString() +"=" + ht[o].ToString() +"\r\n";
  Write line to output
}
close the file.
Of course you should test line for null after each read, and kv array after split()...
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top