0

My earlier post was unreadable so. I am trying to read the last line of a text file every time it changes. The code I have is,

private void fileSystemWatcherMCH1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
    string machState = File.ReadAllLines(@"C:\Users\sgarner\Documents\PROTOMET SHOP FLOOR\Machines\MACHINE_1.txt").Last();
    btnMCH1.Text = machState;
    btnMCH1.BackColor = Color.Blue;
}

If I only run the btnMCH1.BackColor = Color.Blue; it works. But I cannot read in the variable from the text file. I am certain I am missing something simple. Thanks,

chue x
  • 18,573
  • 7
  • 56
  • 70

1 Answers1

0

It seems that your code is raising an exception but for any reason you're not seeing it. Maybe the file is being used by other process ... Try to catch it and then show it, so, you can see the problem:

private void fileSystemWatcherMCH1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
    try
    {
        string machState = File.ReadAllLines(@"C:\Users\sgarner\Documents\PROTOMET SHOP FLOOR\Machines\MACHINE_1.txt").Last();
        btnMCH1.Text = machState;
        btnMCH1.BackColor = Color.Blue;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Messasge);
    }
}
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219