As Ak

As Ak

  • NA
  • 65
  • 7.8k

Display data from latest text file into richtextbox

Sep 30 2020 9:41 PM
I want to display the latest text file that has been created in the richtextbox. The name for each text file is having a datetime.
 
I'm having a problem to display the data from latest txt file into the richtextbox.
 
This is the example of text file that has been created.
 
 
 
This is my code at this moment :
  1. private void CheckSN()  
  2. {  
  3.       string strComputerName = Environment.MachineName.Substring(4);  
  4.       string Path = $@"Z:\FC\Share\PC\TR-{strComputerName}\SS\SN\{strComputerName}_DataFile.txt";  
  5.    string logPath = $@"Z:\FC\Share\PC\TR-{strComputerName}\SS\SysLog\SystemLog_{DateTime.Now:ddMMyyyy-hhmm}.txt";  
  6.    if (File.Exists(Path))  
  7.    {  
  8.    var data = File  
  9.    .ReadAllLines(Path)  
  10.    .Select(x => x.Split('='))  
  11.    .Where(x => x.Length > 1)  
  12.    .ToDictionary(x => x[0].Trim(), x => x[1]);  
  13.    using (StreamWriter tw = File.CreateText(logPath))  
  14.    {  
  15.       tw.WriteLine("Read the details..\n");  
  16.       tw.WriteLine("Name: {0}", data["Name"]);  
  17.       tw.WriteLine("Emp ID {0}", data["EmpID"]);  
  18.       tw.WriteLine("---------------------------------------------\n");  
  19.    }  
  20. }  
  21.    
  22. private void richTextBox1_TextChanged(object sender, EventArgs e)  
  23. {  
  24.     string strComputerName = Environment.MachineName.ToString().Substring(4);  
  25.     string logPath = $@"Z:\FC\Share\PC\TR-{strComputerName}\SS\SysLog\SystemLog.txt";  
  26.     richTextBox1.Text = File.ReadAllText(logPath, Encoding.Default);  
  27. }  
How can I display the latest text file that has been created (SystemLog_01102020-1040) in the richtextbox?

Answers (4)