Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have this DataViewGrid table that when I press Load button on my GUI, it will allow me to load the data of from the text file onto the table. Everything works. Also, during load, I allow user to open up the file for editing that way as well using
C#
Process.Start("notepad.exe",filename)
. Now here is my question:

How do I go about having the table update its content when I edit the file on notepad. For example, if I add the data "abcd" to the file and save it in notepad, the table on my gui would update as well.

Thanks in advance
Posted

1 solution

You have to reload your grid from the text file every time you make changes the file.
use FileSystemWatcher class to trap the changes made to your text file.
C#
public void CreateFileWatcher(string path)
{
    // Create a new FileSystemWatcher and set its properties.
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = path;
    /* Watch for changes in LastAccess and LastWrite times, and 
       the renaming of files or directories. */
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    // Only watch text files.
    watcher.Filter = "*.txt";

    // Add event handlers.
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Deleted += new FileSystemEventHandler(OnChanged);
    watcher.Renamed += new RenamedEventHandler(OnRenamed);

    // Begin watching.
    watcher.EnableRaisingEvents = true;
}

// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
    // Specify what is done when a file is changed, created, or deleted.
   Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}

private static void OnRenamed(object source, RenamedEventArgs e)
{
    // Specify what is done when a file is renamed.
    Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}

From http://stackoverflow.com/questions/721714/notification-when-a-file-changes[^]
 
Share this answer
 
Comments
dbzx734 12-Dec-12 23:50pm    
I am getting "The directory name C:\text.txt is invalid." when I load the file.
Jibesh 12-Dec-12 23:52pm    
can you paste your code so that i can find error if any, you can use 'Improve question' link to update your code
dbzx734 13-Dec-12 0:04am    
I got it. THanks for the help. it wanted the directory instead of the file itself.
Jibesh 13-Dec-12 0:05am    
good to see that you solved yourself the other problems.. enjoy your day!!!
Menon Santosh 13-Dec-12 0:10am    
Good Solution +5

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