You need to understand how WebForms works.
The user makes a request to the server. The server creates an instance of your
class. It runs through the page event lifecycle
] to build up a tree of controls. It then renders those controls to HTML, sends the resulting HTML back to the user, and throws away the page instance.
In your code, you set up a
which, at some unknown point in the future
, will fire an event to tell you that a file has changed.
But by the time that event fires, it's too late. Your page has already been rendered to HTML and sent back to the user. Changing properties of the controls on that discarded page instance will not magically connect to the user and update the HTML they're looking at.
You have a couple of options. Your page could show the current content
of the file when it loads, and wait for the user to refresh the page to see if the content has changed. Or you could use something like SignlaR
] to establish a two-way communication between the page and the server, so that the server can "push" a notification when the file changes.
Using SignalR with WebForms is not trivial, but it can be done. For example:
in an ASP.NET application is pointless. There is no "console" to write to! Either switch to using
, or use a proper logging library.
Adding SignalR to an ASP.NET WebForms Project