IT Community - Software Programming, Web Development and Technical Support

How to watch a file whenever we change or modify the content using c#?

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-30-2007, 12:37 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Question How to watch a file whenever we change or modify the content using c#?

How to watch a file whenever we change or modify the content of the file or rename of the file using C#?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 12:42 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: How to watch a file whenever we change or modify the content using c#?

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);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-09-2007, 06:04 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Wink Re: How to watch a file whenever we change or modify the content using c#?

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-09-2007, 06:20 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Re: How to watch a file whenever we change or modify the content using c#?

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..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-20-2007, 07:25 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: How to watch a file whenever we change or modify the content using c#?

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-20-2007, 07:38 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: How to watch a file whenever we change or modify the content using c#?

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!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 10:49 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0