---
title: "Reading from a textfile to retrieve values using C#"  
description: "Reading from a textfile to retrieve values using C#"  
author: "Manoj Bhatt"  
published: 2013-09-04  
updated: 2015-06-07  
canonical: https://www.mindstick.com/forum/1445/reading-from-a-textfile-to-retrieve-values-using-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Reading from a textfile to retrieve values using C#

Use Notepad to place the following values in a [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java): 86, 97, 144, 26.To simplify the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic), the values can each be placed on separate lines.Write a [C# program](https://www.mindstick.com/forum/156830/how-do-i-make-a-c-sharp-program-that-checks-for-given-array-by-user-which-elements-are-perfect-numbers) to [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) the values from the text file and print the average of the values, formatted with two [decimal places](https://www.mindstick.com/forum/33881/round-double-type-variable-to-two-decimal-places-in-c-sharp).

However, I keep getting [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core) when I compile my code. I have listed my code below for you to view, and to offer me any [feedback](https://www.mindstick.com/articles/12731/the-importance-of-feedback-to-the-user-experience) on what to do to correct my code.

```
   using System;      using System.Collections.Generic;      using System.ComponentModel;      using System.Data;      using System.Drawing;      using System.IO;      using System.Linq;      using System.Text;      using System.Threading.Tasks;      using System.Windows.Forms;namespace ReadTextFileApp{public class ReadTextFileApp : System.Windows.Forms.Form{    public ReadTextFileApp()    {        InitializeComponent();    }    private void ReadTextFileApp_Load(object sender, EventArgs e)    {        string inValue;        string data;        inFile = new
StreamReader("name.txt", true);    }    private void btnDisplayValues_Click(object sender, EventArgs e)    {        string inValue;        int count = 0;        int total = 0;this.lblDisp.Text = "";       
if(File.Exists("name.txt"))    {        try        {            inFile =new StreamReader{"name.txt");            while((inValue = inFile.ReadLine()) |= null)            {               
count++;               
this.lblDisp.Text += " " + inValue;                total+= int.Parse(inValue);            }            average =(double)                total / count;            }           
catch(System.IO.IOException exc)            {               
this.lblDisp.Text = exc.Message;            }        }        else        {           
this.lblDisp.Text = "File Unavailable";        }       
this.lblDisp.Visible = true;    {    private void ReadTextFileApp_Closing(object sender, 
System.ComponentModel.CancelEventArgs e)                                                
    {        try        {           
inFile.Close();        }        catch        {        }    }    private void btnCalcDispAve_Click(object sender, EventArgs e)    {       
this.lblAve.Text = "";       
this.lblAve.Text = "  
" + average.ToString();       
this.lblAve.Visible = true;    }}
```

## Replies

### Reply by Joosh Seerden

```
using System;using System.IO;namespace PRG321WK3Assignment3{    class Program    {        static void Main(string[] args)        {            string inValue;            int count = 0;            double sum = 0;            double average;            try            {                StreamReader inStream = new StreamReader("values.txt");                while ((inValue = inStream.ReadLine()) != null)                {                    sum += Convert.ToInt16(inValue);                    count++;                }                average = sum / count;                Console.WriteLine("The average is: {0:f2}", average);            }            catch (IOException e)            {                Console.WriteLine("Exception!  " + e.Message);            }            Console.Read();        }    }}
```

\

### Reply by Sumit Kesarwani

Hi Manoj,\

You have many, many syntax errors in your [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program)

Where is inFile declared?

inFile = new StreamReader("name.txt", true);

You have a brace instead of a bracket on this line:

inFile = new StreamReader{"name.txt");

You are bitwise OR-EQUALING the string value on this line:

while ((inValue = inFile.ReadLine()) |= null)


---

Original Source: https://www.mindstick.com/forum/1445/reading-from-a-textfile-to-retrieve-values-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
