This is a discussion on How to watch a file whenever we change or modify the content using c#? within the C# Programming forums, part of the Software Development category; How to watch a file whenever we change or modify the content of the file or rename of the file ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| private System.IO.FileSystemWatcher watch; this.watch.NotifyFilter = System.IO.NotifyFilters.Attributes; this.watch.SynchronizingObject = this; this.watch.Created += new System.IO.FileSystemEventHandler(this.watch_Create d); this.watch.Deleted += new System.IO.FileSystemEventHandler(this.watch_Delete d); this.watch.Renamed += new System.IO.RenamedEventHandler(this.watch_Renamed); this.watch.Changed += new System.IO.FileSystemEventHandler(this.watch_change d); // to start watch watch.Path = "test.txt";//file name watch.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite; watch.EnableRaisingEvents = true; private void watch_Created(object sender, System.IO.FileSystemEventArgs e) { lbl_status.Text += e.Name +" "+e.ChangeType.ToString()+"\n"; } private void watch_Deleted(object sender, System.IO.FileSystemEventArgs e) { lbl_status.Text +=" The File '"+ e.Name +"' has been "+e.ChangeType.ToString()+"\n"; MessageBox.Show(" The File '"+ e.Name + "' has been " + e.ChangeType); } private void watch_Renamed(object sender, System.IO.RenamedEventArgs e) { lbl_status.Text += " The File '"+e.OldName +"' renamed to "+e.Name+"\n"; MessageBox.Show(" The File '"+e.OldName + "' renamed to " + e.Name); } |
| |||
| Hi, Can we watch a file in windowsmobile....Is there any sample for filewatcher
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| Hi, Here i am giving the code for watch a file in windows mobile... U can try this in ur program... Here follows, FileSystemWatcher watch = new FileSystemWatcher(); this.watch.EnableRaisingEvents = true; this.watch.NotifyFilter = OpenNETCF.IO.NotifyFilters.Attributes; this.watch.SynchronizingObject = this; this.watch.Changed += new OpenNETCF.IO.FileSystemEventHandler(this.watch_cha nged); this.watch.Deleted += new OpenNETCF.IO.FileSystemEventHandler(this.watch_del eted); this.watch.Renamed += new OpenNETCF.IO.RenamedEventHandler(this.watch_rename d); I am using click event for watch a file Here its, private void Click_Click(object sender, EventArgs e) { string[] tempPath = path1.Split('\\'); string tempVar = null; for (int i = 0; i < tempPath.Length-1; i++) { tempVar += tempPath[i] + "\\"; } this.watch.Path = tempVar; this.watch.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite; this.watch.EnableRaisingEvents = true; Status.Text += "\nstart watching" + path.Text; } private void watch_changed(object sender, OpenNETCF.IO.FileSystemEventArgs e) { path1=e.FullPath; string tempText = null; FileStream fls = new FileStream(path1, FileMode.OpenOrCreate, System.IO.FileAccess.Read); StreamReader sr = new StreamReader(fls); sr.BaseStream.Seek(0, SeekOrigin.Begin); while (sr.Peek() > -1) { tempText = sr.ReadLine(); } sr.Close(); fls.Close(); if (tempText != lastText) { lastText = tempText; MessageBox.Show("The content of '"+e.Name+"' is modified to : "+tempText); } } private void watch_deleted(object sender, OpenNETCF.IO.FileSystemEventArgs e) { MessageBox.Show(e.Name + " has " + e.ChangeType.ToString()); } private void watch_renamed(object sender, OpenNETCF.IO.RenamedEventArgs e) { path.Text += e.OldName + " renamed to " + e.Name + "\n"; MessageBox.Show(e.OldName + " renamed to " + e.Name); }
__________________ H2O Without us, no one can survive.. |
| |||
| Hi, I used the above code... how to create a text file to watch in windowsmobile...Is there any method to view the file
__________________ Krishnakumar.S Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily |
| |||
| If u want to watch a text file means... Create one text file in desktop and then copy and paste it into windows CE remote file viewer... Then you can browse the file in your windowsmobile and then click to watch it. That's it... ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to watch a file using C#? | S.Vinothkumar | C# Programming | 4 | 09-26-2007 12:30 AM |
| How to list the content of jar file in java? | oxygen | Java Programming | 2 | 09-14-2007 12:46 AM |
| How do I change another window's title, that is, the content of title bar at the top | itbarota | HTML, CSS and Javascript Coding Techniques | 1 | 09-13-2007 03:02 AM |
| Change the Asp.net (C#) Web.config file | a.deeban | ASP and ASP.NET Programming | 1 | 08-18-2007 04:42 AM |
| modify a program written in .NET 2.0 | pranky | C# Programming | 1 | 07-12-2007 04:47 AM |