Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2011, 06:22 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 21 Monitoring the File System

in this try it out it don't creates Log.txt file in folder FileLogs and of course don't writes anything in it. can anyone help?

the code is as follows:

Code:
namespace FileWatch
{
    public partial class Form1 : Form
    {
        // File System Watcher object.
        private FileSystemWatcher watcher;
        private delegate void UpdateWatchTextDelegate(string newText);

        public Form1()
        {
            InitializeComponent();

            this.watcher = new FileSystemWatcher();
            this.watcher.Deleted +=
            new FileSystemEventHandler(this.OnDelete);
            this.watcher.Renamed +=
            new RenamedEventHandler(this.OnRenamed);
            this.watcher.Changed +=
            new FileSystemEventHandler(this.OnChanged);
            this.watcher.Created +=
            new FileSystemEventHandler(this.OnCreate);

            //tu ar aris konkretuli (am shemtxvevashi C:\FileLogs) direqtoria 
            //mashin sheiqmneba.
            DirectoryInfo aDir = new DirectoryInfo(@"C:\FileLogs");
            if (!aDir.Exists)
                aDir.Create();
        }

        // Utility method to update watch text.
        public void UpdateWatchText(string newText)
        {
            lblWatch.Text = newText;
        }
        // Define the event handlers.
        public void OnChanged(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} {1}", e.FullPath,
                e.ChangeType.ToString());
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote change event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }
        public void OnRenamed(object source, RenamedEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File renamed from {0} to {1}", e.OldName,
                e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote renamed event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }

        public void OnDelete(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} Deleted", e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote delete event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }
        public void OnCreate(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} Created", e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote create event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            if (FileDialog.ShowDialog() != DialogResult.Cancel)
            {
                txtLocation.Text = FileDialog.FileName;
                cmdWatch.Enabled = true;
            }
        }

        private void cmdWatch_Click(object sender, EventArgs e)
        {
            watcher.Path = Path.GetDirectoryName(txtLocation.Text);
            watcher.Filter = Path.GetFileName(txtLocation.Text);
            watcher.NotifyFilter = NotifyFilters.LastWrite |
            NotifyFilters.FileName | NotifyFilters.Size;
            lblWatch.Text = "Watching " + txtLocation.Text;
            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

        
    }
}

Last edited by samuraisxmali; November 9th, 2011 at 06:26 AM..
 
Old November 9th, 2011, 11:40 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by samuraisxmali View Post
in this try it out it don't creates Log.txt file in folder FileLogs and of course don't writes anything in it. can anyone help?

the code is as follows:

Code:
namespace FileWatch
{
    public partial class Form1 : Form
    {
        // File System Watcher object.
        private FileSystemWatcher watcher;
        private delegate void UpdateWatchTextDelegate(string newText);

        public Form1()
        {
            InitializeComponent();

            this.watcher = new FileSystemWatcher();
            this.watcher.Deleted +=
            new FileSystemEventHandler(this.OnDelete);
            this.watcher.Renamed +=
            new RenamedEventHandler(this.OnRenamed);
            this.watcher.Changed +=
            new FileSystemEventHandler(this.OnChanged);
            this.watcher.Created +=
            new FileSystemEventHandler(this.OnCreate);

            //tu ar aris konkretuli (am shemtxvevashi C:\FileLogs) direqtoria 
            //mashin sheiqmneba.
            DirectoryInfo aDir = new DirectoryInfo(@"C:\FileLogs");
            if (!aDir.Exists)
                aDir.Create();
        }

        // Utility method to update watch text.
        public void UpdateWatchText(string newText)
        {
            lblWatch.Text = newText;
        }
        // Define the event handlers.
        public void OnChanged(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} {1}", e.FullPath,
                e.ChangeType.ToString());
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote change event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }
        public void OnRenamed(object source, RenamedEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File renamed from {0} to {1}", e.OldName,
                e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote renamed event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }

        public void OnDelete(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} Deleted", e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote delete event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }
        public void OnCreate(object source, FileSystemEventArgs e)
        {
            try
            {
                StreamWriter sw =
                new StreamWriter("C:/FileLogs/Log.txt", true);
                sw.WriteLine("File: {0} Created", e.FullPath);
                sw.Close();
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Wrote create event to log");
            }
            catch (IOException)
            {
                this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),
                "Error Writing to log");
            }
        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            if (FileDialog.ShowDialog() != DialogResult.Cancel)
            {
                txtLocation.Text = FileDialog.FileName;
                cmdWatch.Enabled = true;
            }
        }

        private void cmdWatch_Click(object sender, EventArgs e)
        {
            watcher.Path = Path.GetDirectoryName(txtLocation.Text);
            watcher.Filter = Path.GetFileName(txtLocation.Text);
            watcher.NotifyFilter = NotifyFilters.LastWrite |
            NotifyFilters.FileName | NotifyFilters.Size;
            lblWatch.Text = "Watching " + txtLocation.Text;
            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

        
    }
}
What do you want to know? The sample creates a file system watcher to monitor changes on the file system. Step 14 of the excercise on page 720 asks to create a file and add some text to the file using Notepad. Step 15 asks for renaming the previously created file. Have you been successful with the Explorer and Notepad? Then the events from the file system watcher should occur.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old November 9th, 2011, 02:23 PM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by ChristianNagel View Post
What do you want to know? The sample creates a file system watcher to monitor changes on the file system. Step 14 of the excercise on page 720 asks to create a file and add some text to the file using Notepad. Step 15 asks for renaming the previously created file. Have you been successful with the Explorer and Notepad? Then the events from the file system watcher should occur.
yes of course, i did as it is in the book, but it not writes anything in Log.txt file because it (Log.txt file) don't creates at all. the folder you are talking about is TempWatch directory and temp.txt file in it, not FileLogs directory. i said that StreamWriter not works and not creates C:\FileLogs\Log.txt file, which must be created when it is called. i also downloaded the code from this site, but it not works too. i want to know why it not creates Log.txt file in FileLogs directory? maybe somewhere must be the EndInvoke method?
i'm on windows xp sp3.
sorry for bad english and thanks for answer.
 
Old November 9th, 2011, 07:42 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Did you verify if any of the file system watcher event handler is invoked while you run the application? If it is not try changing the Filter property of the FileSystemWatcher to "*.txt" instead of the filename.

Quote:
Originally Posted by samuraisxmali View Post
yes of course, i did as it is in the book, but it not writes anything in Log.txt file because it (Log.txt file) don't creates at all. the folder you are talking about is TempWatch directory and temp.txt file in it, not FileLogs directory. i said that StreamWriter not works and not creates C:\FileLogs\Log.txt file, which must be created when it is called. i also downloaded the code from this site, but it not works too. i want to know why it not creates Log.txt file in FileLogs directory? maybe somewhere must be the EndInvoke method?
i'm on windows xp sp3.
sorry for bad english and thanks for answer.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old November 10th, 2011, 06:21 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by ChristianNagel View Post
Did you verify if any of the file system watcher event handler is invoked while you run the application? If it is not try changing the Filter property of the FileSystemWatcher to "*.txt" instead of the filename.
i tried it and changed filter but result is same - nothing happens.

i debugged it step by step and none of filesystemwatcher event hendlers are invoked, it gets on initializations
Code:
 this.watcher = new FileSystemWatcher();
            this.watcher.Deleted +=
            new FileSystemEventHandler(this.OnDelete);
            this.watcher.Renamed +=
            new RenamedEventHandler(this.OnRenamed);
            this.watcher.Changed +=
            new FileSystemEventHandler(this.OnChanged);
            this.watcher.Created +=
            new FileSystemEventHandler(this.OnCreate);
but not exactly event handlers. even when i changed the filter. i tried everything i know, but...
 
Old November 10th, 2011, 06:27 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by ChristianNagel View Post
Did you verify if any of the file system watcher event handler is invoked while you run the application? If it is not try changing the Filter property of the FileSystemWatcher to "*.txt" instead of the filename.
changed filter but the result is same.
i debugged step by step and none of event handlers are invoking, it gets only initializations such as
Code:
 this.watcher = new FileSystemWatcher();
            this.watcher.Deleted +=
            new FileSystemEventHandler(this.OnDelete);
            this.watcher.Renamed +=
            new RenamedEventHandler(this.OnRenamed);
            this.watcher.Changed +=
            new FileSystemEventHandler(this.OnChanged);
            this.watcher.Created +=
            new FileSystemEventHandler(this.OnCreate);
i don't know what to think. i tried everything i knew. can you test it to see whats wrong? thanks.
 
Old November 13th, 2011, 01:19 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by samuraisxmali View Post
changed filter but the result is same.
i debugged step by step and none of event handlers are invoking, it gets only initializations such as
Code:
 this.watcher = new FileSystemWatcher();
            this.watcher.Deleted +=
            new FileSystemEventHandler(this.OnDelete);
            this.watcher.Renamed +=
            new RenamedEventHandler(this.OnRenamed);
            this.watcher.Changed +=
            new FileSystemEventHandler(this.OnChanged);
            this.watcher.Created +=
            new FileSystemEventHandler(this.OnCreate);
i don't know what to think. i tried everything i knew. can you test it to see whats wrong? thanks.
I've already copied the code you posted, just changed the filter to *.txt, and everything is working. Of course only if you create a new file in the directory that is referenced by the filter, if you change, rename, and delete the file.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old December 16th, 2011, 07:45 AM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default

For me everything works fine too,with initial filter code:
Code:
Watcher.Filter=Path.GetFileName(textBox1.Text)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Health Monitoring System vic02 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 February 7th, 2007 11:52 AM
Configuring the Health Monitoring System hoouliganian BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 January 30th, 2007 09:00 PM
Health Monitoring System spardoe BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 October 31st, 2006 10:28 PM
Need Help with Health Monitoring System ryandoah BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 8 October 19th, 2006 08:11 AM
Health Monitoring System - Chapter 3 Page 109 - 11 Laurie S. Stapleton BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 August 31st, 2006 12:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.