mouthpiec
mouthpiec

Reputation: 4033

Reading parameters from External file - C#

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?

Upvotes: 5

Views: 9824

Answers (3)

Robben_Ford_Fan_boy
Robben_Ford_Fan_boy

Reputation: 8730

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039248

var settings = 
     from line in File.ReadAllLines("params.txt")
     let parameters = line.Split('=')
     select new KeyValuePair<string, string>(parameters[0], parameters[1]);

Upvotes: 8

Joey
Joey

Reputation: 354734

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

Upvotes: 1

Related Questions