Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a code that read in a richtextbox the last row from a txt file.
What I want to introduce now is a refresh button.
I want to put it so I don't have to use everytime the open file dialog to read last row of the same file, but if I have this refresh button, the program just call last file opened with open file dialog, refresh it on richtextbox, so if there are some new rows it will let me always the last but of that moment.

What I have tried:

This is what I did, but have no idea how should the code be for make this refresh button:
C#
public async void OpenFileBtn_ClickAsync(object sender, EventArgs e)
        {
             using(OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text File|*.txt", Multiselect = false })
             {

                 if (ofd.ShowDialog() == DialogResult.OK)
                 {
                    using (StreamReader rd = new StreamReader(ofd.FileName))
                    {
                        //ReaderRichTxtBox.Text = await rd.ReadToEndAsync();
                        string[] lines = rd.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                        ReaderRichTxtBox.Text = lines[lines.Length - 1];
                    }
                 }
             }

        }


        private void RefreshBtn_Click(object sender, EventArgs e)
        {
            ..........................................

        }
    }
}
Posted
Updated 28-May-20 5:04am
v2

You could create a class level string variable holding the current path. Then set this variable to whatever path has been selected in the OpenFileDialog.
Then, in the refresh handler, apply your refresh logic to stored path.
C#
private string thePath;

public async void OpenFileBtn_ClickAsync(object sender, EventArgs e)
{
   using(OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text File|*.txt", Multiselect = false })
   {

      if (ofd.ShowDialog() == DialogResult.OK)
      {
         thePath = ofd.FileName;
         Refresh();
      }
   }
}


private void RefreshBtn_Click(object sender, EventArgs e)
{
   Refresh();
}

private void Refresh()
{
   using (StreamReader rd = new StreamReader(thePath))
   {
      //ReaderRichTxtBox.Text = await rd.ReadToEndAsync();
      string[] lines = rd.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
      ReaderRichTxtBox.Text = lines[lines.Length - 1];
   }
} 
 
Share this answer
 
v3
Comments
Member 14786879 28-May-20 10:15am    
And if I want to do something like:
- Put a text box where i set a time (in minutes).
- Another button that when pressed it refresh every minutes that i wrote before in the text box?
- Then a stop refresh button
phil.o 28-May-20 10:40am    
The requirement is not totally clear to me. Besides, it goes beyond your original question. Please, try to do it by yourself first, then ask a new question about this new requirement if you are stuck, eventually. Sorry, but I won't write it all for you.
Put this part in a separate method which you can call from both your existing methods:
using (StreamReader rd = new StreamReader(ofd.FileName))
{
    //ReaderRichTxtBox.Text = await rd.ReadToEndAsync();
    string[] lines = rd.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    ReaderRichTxtBox.Text = lines[lines.Length - 1];
}

So that would look like this:
private void ReadLastLineFromFile(string fileName)
{
                    using (StreamReader rd = new StreamReader(fileName))
                    {
                        string[] lines = rd.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                        ReaderRichTxtBox.Text = lines[lines.Length - 1];
                    }
}
 
Share this answer
 
v2
Please, read the comment to the answer: How do I read last line of a text file selected with this code? Or with another mode?[^]

Richard Deeming wrote:


Rather than reading the entire file into an array, I'd be inclined to use ReadLines and LastOrDefault. :)

C#
string lastLine = File.ReadLines("fullfilename.txt").LastOrDefault();


File.ReadLines Method (System.IO) | Microsoft Docs[^]
 
Share this answer
 
Comments
phil.o 28-May-20 14:29pm    
5'd
Maciej Los 28-May-20 14:43pm    
Thanks ;)

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