tags:

views:

74

answers:

3

I am writing an application using C# and I would like to read some parameters from an external file like for example a text file. The parameters will be saved in the file in the form of

parA = 5
parB = hello
etc

Can you pleas suggest a way how I can do this?

+1  A: 

Read each line and split it at the first occurrence of "=".

Joey
I would say split at the first occurrence of " = ", or trim the results of the array.
Matt Ellen
@Matt: depending on whether the whitespace there is significant. But yes, I kinda assumed that the elements would be trimmed, then. But the question itself was too obvious already, actually.
Joey
+1  A: 
var settings = 
     from line in File.ReadAllLines("params.txt")
     let parameters = line.Split('=')
     select new KeyValuePair<string, string>(parameters[0], parameters[1]);
Darin Dimitrov
+1  A: 

I know its not what you specifically asked, but if you have the choice I would go with an XML Application config.

There's plenty of resources on it but here's a fairly straight forward example:

http://www.c-sharpcorner.com/UploadFile/dolson/XMLConfigInWinForms11262005014845AM/XMLConfigInWinForms.aspx

David Relihan
good idea ... once i used XML .... and for sure it will give me some extra marks
mouthpiec
@Mouthpiec I would agree - In my opinion not only are you showing you can do the question but you are demonstrating that you are thinking about design choices too
David Relihan