Click here to Skip to main content
15,881,092 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
 
Share this answer
 
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?
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;
 
Share this answer
 

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