Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to read and write the first three lines of a text file to my WPF application. I wrote it down with the code shown below, but the textblock still doesn't show me any values.

What I have tried:

C#
private List<string> line;
private int currentLine;
readonly string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\rank.txt";
public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            firstTime();
            timeFirst.Text = line[currentLine];
            secondTime();
            timeSecond.Text = line[currentLine];
            thirdTime();
            timeThird.Text = line[currentLine];
        }

private void otherPositions_Click(object sender, RoutedEventArgs e)
        {
            var p = new Process();
            p.StartInfo = new ProcessStartInfo(path)
            {
                UseShellExecute = true
            };
            p.Start();
        }
private List<string> firstTime()
        {
            var streamReader = new StreamReader(path);
            streamReader.ReadLine();
            line = new List<string>();
            string lineS;
            for (int i = 0; i < 3; i++)
            {
                lineS = streamReader.ReadLine();
                line.Add(lineS);

                if (streamReader.EndOfStream == true)
                {
                    break;
                }
            }
            return line;
            
            
        }
private void secondTime()
        {
            if (currentLine == line.Count - 1) return;
            currentLine++;
        }
private void thirdTime()
        {
            if (currentLine == line.Count - 1) return;
            currentLine++;
        }

EDIT:
C#
public partial class Rank : Window
   {
       private List<string> line;
       private int currentLine;
       readonly string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\rank.txt";
       private string measuredTime;
       private string _firstTime;
       public Rank(TimeSpan time)
       {
           InitializeComponent();
           measuredTime = time.ToString();
       }


       public void Window_Loaded(object sender, RoutedEventArgs e)
       {
           firstTime();
           timeFirst.Text = _firstTime;
           secondTime();
           timeSecond.Text = line[currentLine];
           thirdTime();
           timeThird.Text = line[currentLine];
           timeYour.Text = measuredTime;
       }

       private void otherPosition_Click(object sender, RoutedEventArgs e)
       {
           var p = new Process();
           p.StartInfo = new ProcessStartInfo(path)
           {
               UseShellExecute = true
           };
           p.Start();
       }
       private List<string> firstTime()
       {
           var streamReader = new StreamReader(path);
           streamReader.ReadLine();
           line = new List<string>();
           string lineS;
           for (int i = 0; i < 3; i++)
           {
               lineS = streamReader.ReadLine();
               line.Add(lineS);

               if (streamReader.EndOfStream == true)
               {
                   break;
               }
           }
           return line;
           foreach (string s in line)
           {
               _firstTime = s;
           }

       }
       private void secondTime()//this is old
       {
           if (currentLine == line.Count - 1) return;
           currentLine++;
       }
       private void thirdTime()//this is old too
       {
           if (currentLine == line.Count - 1) return;
           currentLine++;
       }
   }

timeFirst, timeSecond, timeThird these are textblock
everything happens normally but the value timeFirst.Text = null(debbuger shows)
If anyone knows of a simpler solution or has advice on why this doesn't work, I'll be very happy.
Thank you
Posted
Updated 18-Jun-21 2:17am
v4

C#
streamReader.ReadToEnd(); // file is now at end of file
line = new List<string>();
string lineS;
while ((lineS = streamReader.ReadLine()) != null) // so nothing more to read.

Also:
C#
currentLine = 0;
if (currentLine == 1) return;
currentLine--; // currentLine will always be -1
   
Comments
Member 15170612 18-Jun-21 4:11am
   
I edited my code but now for a change shows timeFirst.Text to the second line
Richard MacCutchan 18-Jun-21 6:09am
   
You are doing it again:
// this code makes no sense
            currentLine = -1; // currentLine equals -1
            if (currentLine == 0) return; // so it can never equal zero
            currentLine++;

However that is somewhat by the way. If you want to read the first three lines then do it in a single method:
Create the List object that will hold the strings
For integer 1 to 3 
    Read the next line from the file
    Add it to the list
    If End-Of-File is TRUE
        Break
End For
Return the List of strings
Member 15170612 18-Jun-21 6:35am
   
 var streamReader = new StreamReader(path);
            streamReader.ReadLine();
            line = new List<string>();
            string lineS;
            for (int i = 0; i < 3; i++)
            {
                lineS = streamReader.ReadLine();
                line.Add(lineS);

                if (streamReader.EndOfStream == true)
                {
                    break;
                }
            }
            return;
Richard MacCutchan 18-Jun-21 7:03am
   
You need to return the List to the caller.
Member 15170612 18-Jun-21 7:15am
   
And how can I write the three lines from that list now?
Richard MacCutchan 18-Jun-21 7:27am
   
foreach (string s in list)
{
    // write the string
}
Member 15170612 18-Jun-21 7:52am
   
It returns the value of s as null, do you know why?
Richard MacCutchan 18-Jun-21 8:00am
   
Probably because you never put anything in it. You really need to provide proper details when something does not work; we cannot guess what you are doing.
Member 15170612 18-Jun-21 8:04am
   
The document must read this because my first solution listed values, now according to debbuger the value "s" is null and List line count = 3 so the value has.
Richard MacCutchan 18-Jun-21 8:05am
   
As I said, we cannot guess what is happening. Please show the code where you create the list, and the code where the error occurs.
Member 15170612 18-Jun-21 8:17am
   
I improved the question
Richard MacCutchan 18-Jun-21 9:19am
   
And it still looks like you are just throwing random lines of code together. All you need to load up your data is something like:
    private List<string> timeList; // use a more meaningful name

    public void Window_Loaded(object sender, RoutedEventArgs e)
    {
        timeList = readTimes();
        // you may want to add a check here that you actually got three lines     
        timeFirst.Text = timeList[0]); // add the lines to the textboxes
        timeSecond.Text = timeList[1]);
        timeThird.Text = timeList[2]);

        timeYour.Text = measuredTime;
    }

    private List<string> readTimes()
    {
        List<string> times = new List<string>();
        // A using block is more efficient
        using (var streamReader = new StreamReader(path))
        {
            string newTime; // a more meaningful name
            for (int i = 0; i < 3; i++)
            {
                newTime = streamReader.ReadLine();
                times.Add(newTime);

                if (streamReader.EndOfStream == true)
                {
                    break;
                }
            }
        }
        return times;
    }
Member 15170612 21-Jun-21 2:16am
   
This is perfect!!! Thank you very much!
Richard MacCutchan 21-Jun-21 4:05am
   
You are welcome.
Richard MacCutchan 21-Jun-21 8:22am
   
As with everything in computing you must add the code to check what you are reading. What I wrote above is a very simplified piece of code that assumes the input is all correct. But in the real world your users may pass bad data everywhere so you need to check everything as you read it.
If you are going to read all the lines anyway, why not just use:
C#
string[] lines = File.ReadAllLines(pathToFile);


And what is this code going to do:
C#
currentLine = 0;
if (currentLine == 1) return;
currentLine--;

That is the equivelant of just writing this:
C#
currentLine = -1;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900