file - listbox refresh automatically using filewatcher C# -


ok have been @ awhile. have program monitors 1 file "lab.txt" changes, , when changes, want reload contents of file listbox displayed. can display , tell me when there change, cant listbox refresh. apprieciated. there alot of code not being used because have been trying different methods please disregard.

namespace filechangenotifier {  public partial class frmnotifier : form {     private stringbuilder m_sb;     private bool m_bdirty;     private system.io.filesystemwatcher m_watcher;     private bool m_biswatching;     public static string txtpath = "e:/lab.txt";     list<string> list;     bindinglist<string> bindinglist;      public frmnotifier()     {         initializecomponent();         m_sb = new stringbuilder();         m_bdirty = false;         m_biswatching = false;         //bindingsource bindingsource = (bindingsource)listbox1.datasource;        // list sourcelist = (list)bindingsource.list;         list= new list<string>(file.readlines(txtpath));         bindinglist = new bindinglist<string>(list);         listbox1.datasource = bindinglist;         //listbox1.selectedindex = -1;          m_biswatching = true;         btnwatchfile.backcolor = color.red;         m_watcher = new system.io.filesystemwatcher();          m_watcher.filter = "lab.txt";         m_watcher.path = "e:\\";           m_watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite                              | notifyfilters.filename | notifyfilters.directoryname;         m_watcher.changed += new filesystemeventhandler(onchanged);         m_watcher.created += new filesystemeventhandler(onchanged);         m_watcher.deleted += new filesystemeventhandler(onchanged);         m_watcher.renamed += new renamedeventhandler(onrenamed);         m_watcher.enableraisingevents = true;     }      private void btnwatchfile_click(object sender, eventargs e)     {         if (m_biswatching)         {             m_biswatching = false;             m_watcher.enableraisingevents = false;             m_watcher.dispose();             btnwatchfile.backcolor = color.lightskyblue;             btnwatchfile.text = "start watching";          }         else         {             m_biswatching = true;             btnwatchfile.backcolor = color.red;             btnwatchfile.text = "stop watching";              m_watcher = new system.io.filesystemwatcher();           //if (rdbdir.checked)            //                 m_watcher.filter = "lab.txt";                 m_watcher.path = "e:\\";             //            /*lse             {                 m_watcher.filter = txtfile.text.substring(txtfile.text.lastindexof('\\') + 1);                 m_watcher.path = txtfile.text.substring(0, txtfile.text.length - m_watcher.filter.length);             }              if (chksubfolder.checked)             {                 m_watcher.includesubdirectories = true;             }*/              m_watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite                                  | notifyfilters.filename | notifyfilters.directoryname;             m_watcher.changed += new filesystemeventhandler(onchanged);             m_watcher.created += new filesystemeventhandler(onchanged);             m_watcher.deleted += new filesystemeventhandler(onchanged);             m_watcher.renamed += new renamedeventhandler(onrenamed);             m_watcher.enableraisingevents = true;         }     }      private void onchanged(object sender, filesystemeventargs e)     {         if (!m_bdirty)         {             m_sb.remove(0, m_sb.length);             m_sb.append(e.fullpath);             m_sb.append(" ");             m_sb.append(e.changetype.tostring());             m_sb.append("    ");             m_sb.append(datetime.now.tostring());             m_bdirty = true;            }     }      private void onrenamed(object sender, renamedeventargs e)     {         if (!m_bdirty)         {             m_sb.remove(0, m_sb.length);             m_sb.append(e.oldfullpath);             m_sb.append(" ");             m_sb.append(e.changetype.tostring());             m_sb.append(" ");             m_sb.append("to ");             m_sb.append(e.name);             m_sb.append("    ");             m_sb.append(datetime.now.tostring());             m_bdirty = true;          }                 }      private void tmreditnotify_tick(object sender, eventargs e)     {         if (m_bdirty)         {             lstnotification.beginupdate();             lstnotification.items.add(m_sb.tostring());             lstnotification.endupdate();             m_bdirty = false;         }     }      private void btnbrowsefile_click(object sender, eventargs e)     {      }      private void btnlog_click(object sender, eventargs e)     {         dialogresult resdialog = dlgsavefile.showdialog();         if (resdialog.tostring() == "ok")         {             fileinfo fi = new fileinfo(dlgsavefile.filename);             streamwriter sw = fi.createtext();             foreach (string sitem in lstnotification.items)             {                 sw.writeline(sitem);             }             sw.close();         }     }       public void binddata()     {             listbox1.datasource = null;         bindinglist=null;         list = new list<string>(file.readlines(txtpath));         bindinglist = new bindinglist<string>(list);         listbox1.datasource = bindinglist;      }  private void execute(object sender, eventargs e)     {         string task = taskque.pop();         //execute task;         listbox1.datasource = taskque.gettasks();     }      private void addtask(object sender, eventargs e)     {         taskque.push(listbox1.text);         listbox1.datasource = taskque.gettasks();      }      private void txtfile_textchanged(object sender, eventargs e)     {      }      private void lstnotification_selectedindexchanged(object sender, eventargs e)     {       }      private void listbox1_selectedindexchanged(object sender, eventargs e)     {      } }  public class taskque {     public static string txtpath = "e:/lab.txt";      public static string pop()     {         streamreader sr = new streamreader(txtpath);         string result = sr.readline();         string remaining = sr.readtoend();         sr.close();         streamwriter sw = new streamwriter(txtpath, false);         sw.write(remaining);         sw.close();         return result;     }      public static void push(string s)     {          streamwriter sw = new streamwriter(txtpath, true);         sw.writeline(s);         sw.close();     }      public static ienumerable<string> gettasks()     {         return new list<string>(file.readlines(txtpath));      } }      } 

edit:

this have changed , still no go

 private void onchanged(object sender, filesystemeventargs e)     {         if (!m_bdirty)         {             m_sb.remove(0, m_sb.length);             m_sb.append(e.fullpath);             m_sb.append(" ");             m_sb.append(e.changetype.tostring());             m_sb.append("    ");             m_sb.append(datetime.now.tostring());             m_bdirty = true;             list = new list<string>(file.readalllines(txtpath));             bindinglist = new bindinglist<string>(list);             listbox1.datasource = bindinglist;         }     } 

this form.designer.cs file

namespace filechangenotifier { partial class frmnotifier {     /// <summary>     /// required designer variable.     /// </summary>     private system.componentmodel.icontainer components = null;      /// <summary>     /// clean resources being used.     /// </summary>     /// <param name="disposing">true if managed resources should disposed; otherwise, false.</param>     protected override void dispose(bool disposing)     {         if (disposing && (components != null))         {             components.dispose();         }         base.dispose(disposing);     }      #region windows form designer generated code      /// <summary>     /// required method designer support - not modify     /// contents of method code editor.     /// </summary>     private void initializecomponent()     {         this.components = new system.componentmodel.container();         this.btnwatchfile = new system.windows.forms.button();         this.lstnotification = new system.windows.forms.listbox();         this.label3 = new system.windows.forms.label();         this.tmreditnotify = new system.windows.forms.timer(this.components);         this.dlgopenfile = new system.windows.forms.openfiledialog();         this.dlgopendir = new system.windows.forms.folderbrowserdialog();         this.btnlog = new system.windows.forms.button();         this.dlgsavefile = new system.windows.forms.savefiledialog();         this.listbox1 = new system.windows.forms.listbox();         this.frmnotifierbindingsource = new system.windows.forms.bindingsource(this.components);         ((system.componentmodel.isupportinitialize)(this.frmnotifierbindingsource)).begininit();         this.suspendlayout();         //          // btnwatchfile         //          this.btnwatchfile.backcolor = system.drawing.color.lightskyblue;         this.btnwatchfile.flatstyle = system.windows.forms.flatstyle.popup;         this.btnwatchfile.forecolor = system.drawing.systemcolors.controltext;         this.btnwatchfile.location = new system.drawing.point(15, 165);         this.btnwatchfile.margin = new system.windows.forms.padding(4, 4, 4, 4);         this.btnwatchfile.name = "btnwatchfile";         this.btnwatchfile.size = new system.drawing.size(159, 28);         this.btnwatchfile.tabindex = 4;         this.btnwatchfile.text = "start watching";         this.btnwatchfile.usevisualstylebackcolor = false;         this.btnwatchfile.click += new system.eventhandler(this.btnwatchfile_click);         //          // lstnotification         //          this.lstnotification.formattingenabled = true;         this.lstnotification.itemheight = 16;         this.lstnotification.location = new system.drawing.point(15, 228);         this.lstnotification.margin = new system.windows.forms.padding(4, 4, 4, 4);         this.lstnotification.name = "lstnotification";         this.lstnotification.size = new system.drawing.size(613, 276);         this.lstnotification.tabindex = 5;         this.lstnotification.selectedindexchanged += new system.eventhandler(this.lstnotification_selectedindexchanged);         //          // label3         //          this.label3.autosize = true;         this.label3.font = new system.drawing.font("microsoft sans serif", 8.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(0)));         this.label3.location = new system.drawing.point(15, 208);         this.label3.margin = new system.windows.forms.padding(4, 0, 4, 0);         this.label3.name = "label3";         this.label3.size = new system.drawing.size(158, 17);         this.label3.tabindex = 6;         this.label3.text = "change notifications";         //          // tmreditnotify         //          this.tmreditnotify.enabled = true;         this.tmreditnotify.tick += new system.eventhandler(this.tmreditnotify_tick);         //          // dlgopendir         //          this.dlgopendir.rootfolder = system.environment.specialfolder.mycomputer;         //          // btnlog         //          this.btnlog.backcolor = system.drawing.systemcolors.highlight;         this.btnlog.flatstyle = system.windows.forms.flatstyle.popup;         this.btnlog.location = new system.drawing.point(15, 519);         this.btnlog.margin = new system.windows.forms.padding(4, 4, 4, 4);         this.btnlog.name = "btnlog";         this.btnlog.size = new system.drawing.size(159, 28);         this.btnlog.tabindex = 9;         this.btnlog.text = "dump log";         this.btnlog.usevisualstylebackcolor = false;         this.btnlog.click += new system.eventhandler(this.btnlog_click);         //          // dlgsavefile         //          this.dlgsavefile.defaultext = "log";         this.dlgsavefile.filter = "logfiles|*.log";         //          // listbox1         //          this.listbox1.datasource = this.frmnotifierbindingsource;         this.listbox1.formattingenabled = true;         this.listbox1.itemheight = 16;         this.listbox1.location = new system.drawing.point(807, 74);         this.listbox1.margin = new system.windows.forms.padding(4, 4, 4, 4);         this.listbox1.name = "listbox1";         this.listbox1.size = new system.drawing.size(277, 388);         this.listbox1.tabindex = 10;         this.listbox1.selectedindexchanged += new system.eventhandler(this.listbox1_selectedindexchanged);         //          // frmnotifierbindingsource         //          this.frmnotifierbindingsource.datasource = typeof(filechangenotifier.frmnotifier);         //          // frmnotifier         //          this.autoscaledimensions = new system.drawing.sizef(8f, 16f);         this.autoscalemode = system.windows.forms.autoscalemode.font;         this.clientsize = new system.drawing.size(1207, 562);         this.controls.add(this.listbox1);         this.controls.add(this.btnlog);         this.controls.add(this.label3);         this.controls.add(this.lstnotification);         this.controls.add(this.btnwatchfile);         this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;         this.margin = new system.windows.forms.padding(4, 4, 4, 4);         this.maximizebox = false;         this.name = "frmnotifier";         this.text = "file/directory change notifier";         ((system.componentmodel.isupportinitialize)(this.frmnotifierbindingsource)).endinit();         this.resumelayout(false);         this.performlayout();      }      #endregion      private system.windows.forms.button btnwatchfile;     private system.windows.forms.listbox lstnotification;     private system.windows.forms.label label3;     private system.windows.forms.timer tmreditnotify;     private system.windows.forms.openfiledialog dlgopenfile;     private system.windows.forms.folderbrowserdialog dlgopendir;     private system.windows.forms.button btnlog;     private system.windows.forms.savefiledialog dlgsavefile;     private system.windows.forms.listbox listbox1;     private system.windows.forms.bindingsource frmnotifierbindingsource; } 

}

your current code not compiled. readline should readalllines.

and there no code set listbox value, listbox value not changed.

try using code:

  private void onchanged(object sender, filesystemeventargs e)   {        if (!m_bdirty)        {              m_sb.remove(0, m_sb.length);              m_sb.append(e.fullpath);              m_sb.append(" ");              m_sb.append(e.changetype.tostring());              m_sb.append("    ");              m_sb.append(datetime.now.tostring());              m_bdirty = true;              list = new list<string>(file.readalllines(txtpath));              bindinglist = new bindinglist<string>(list);              listbox1.datasource = bindinglist;         }     } 

update

after check full code, seems you've wrong in code (frmnotifier.cs):

  m_biswatching = true; 

that code cause filewatcher event handler not registered. try debug it..


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -