Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a FileSystemWatcher which monitors a directory for text files and when found, it shows part of the text in the textbox. I tried adding a breakpoint and saw that the .Text property has the desired value, but I'm not able to see it on the client side(webform).

What I have tried:

ASP.NET
<asp:TextBox ID="txtName" runat="server" EnableViewState="true"></asp:TextBox>


C#
FileSystemWatcher watcher;
        protected void Page_Load(object sender, EventArgs e)
        {
            watcher = new FileSystemWatcher(path);
            watcher.Created += OnFileCreated;
            watcher.Changed += OnFileCreated;
            watcher.InternalBufferSize = 64000;
            watcher.Filter = "*.txt";
            watcher.EnableRaisingEvents = true;
        }
        
        private void OnFileCreated(object sender, FileSystemEventArgs e)
        {
            if(e.Name == fileName)
            {
                try
                {
                    if(File.Exists(fileName))
                    {
                        string[] lines = File.ReadAllLines(fileName);

                        if (lines.Length >= 3)
                        {
                            if (lines[1] == string.Empty || lines[1] == null)
                            {
                                string message = "Not a valid value";
                                ClientScript.RegisterStartupScript
                                (this.GetType(), "alert", 
                                 "alert('" + message + "');", true);
                            }
                            else
                            {
                                string name = lines[1];
                                txtname.Text = name;
                            }
                        }
                        else
                        {
                            Console.WriteLine("The file does not 
                                    have at least 3 lines.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorTxt.Text = ex.ToString();
                }
            }
        }
Posted
Updated 27-Oct-23 4:26am
v2

The FSW is telling you when the file is CREATED. That does NOT mean the content of the file is completely written. You have to wait until the file is closed before you can read all of the content.

Whether you use a delay, or a flag set by the writing process, or try to open the file for exclusive (not shared) read access to it, repeating this until you can open it, is up to your business process and the other process doing the writing.
 
Share this answer
 
Comments
Member 14623639 27-Oct-23 9:47am    
I tried adding a break point on the variable at txtname.Text = name; and there I saw that name contains data so that the retrieval of the text is done here but textbox doesn't change it's value. I'm doing this in webform.
Dave Kreskowiak 27-Oct-23 10:20am    
Well, I just told you the downside of using an FSW. The problem you have now is, like Richard said below, you're not understanding the basics of how web pages and servers work. It's a request/response system. The browser makes a request and the server returns a page. The server then forgets the request ever happened.

You do NOT get to write your server-side code to try to update the page. First, which page instance are you talking about? Is it the client that requested it a minute ago, or the one who requested it an hour ago? You are writing your code like a desktop application, completely ignoring the disconnected nature of web applications.
You need to understand how WebForms works.

The user makes a request to the server. The server creates an instance of your Page 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 FileSystemWatcher 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.

NB: Using SignalR with WebForms is not trivial, but it can be done. For example:

NB2: Using Console.WriteLine in an ASP.NET application is pointless. There is no "console" to write to! Either switch to using Debug.WriteLine, or use a proper logging library.
Adding SignalR to an ASP.NET WebForms Project[^]
 
Share this answer
 
v2

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