FileSystemWatcher

Page 1 of 1

7 Replies - 665 Views - Last Post: 23 August 2011 - 08:15 PM Rate Topic: -----

#1 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

FileSystemWatcher

Posted 07 February 2011 - 03:35 PM

I'm fooling around with the FileSystemWatcher in 4.0. I find this very useful but am getting caught in a loop. I'm trying to monitor whenever an ini is changed and change it back to the correct default (long story) however the change event copying over the new file is causing it to drop into a loop ... Any Ideas > ? I played around with the idea of deleting and recreating thefile to avoid triggering the changed event but this leads to another set of issues with the program that I'd rather avoid. Also I'd imagine I could overwrite the text but this also poses the same issue. Thanks in advance for the help


      static void Main() { Watch (@"\\NoFault2010\Lexis\Data\Setup\", "tmconfig.ini", true); }
        
            static void Watch (string path, string filter, bool includeSubDirs)
            {
                using (var watcher = new FileSystemWatcher (path, filter))
                {

                    watcher.Changed += FileChanged;
                    
                    watcher.EnableRaisingEvents = true;

                    Console.WriteLine("Do Not Close ... \n\nThis is a Temporary Configuration Manager for Time Matters ... \n\n\nI'm Listening ............");
                    Console.ReadLine();
                }
            }

        static void FileChanged (object o, FileSystemEventArgs e)
        {
            string _right_stuff = @"\\NOFAULT2010\Lexis\Data\Templates\Programs\tmconfig.ini";
            string _working = @"\\NOFAULT2010\Lexis\Data\Setup\tmconfig.ini";

            System.Threading.Thread.Sleep(2000);

            File.Copy(_right_stuff, _working, true);
            
            Console.WriteLine("File {0} has been {1}", e.FullPath, e.ChangeType);
            MAIL_IT("SQLMail@lcjlawfirm.com", "TM Master.INI has been altered", "Check the Master INI and Yell At Ecopy Guy " + e.ChangeType + e.FullPath);
            
        }



This post has been edited by d_rop4nme: 07 February 2011 - 03:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: FileSystemWatcher

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,186
  • Joined: 02-June 10

Re: FileSystemWatcher

Posted 07 February 2011 - 03:39 PM

Quote

I'm trying to monitor whenever an ini is changed and change it back to the correct default (long story) however the change event copying over the new file is causing it to drop into a loo
Recognize a change causes make a change causes recognize a change causes make a change. yeah, duh.

When you respond to the first change, unsubscribe from the event. Then make your changes. Then re-subscribe.
Was This Post Helpful? 1
  • +
  • -

#3 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

Re: FileSystemWatcher

Posted 07 February 2011 - 05:24 PM

wow, I haven't been duh'ed in a while thanks that's humbling lol ... You mean like -= something >? ahh ... pardon my ignorance but I'm still learning, how would I unsubscribe from the event handler from outside of the original method ?

Thanks for the Reply

This post has been edited by d_rop4nme: 07 February 2011 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4574
  • View blog
  • Posts: 8,013
  • Joined: 08-June 10

Re: FileSystemWatcher

Posted 07 February 2011 - 05:42 PM

Well, you can't from outside the scope the watcher was declared in. So you'll have to make the watcher global. And then yes, you unsubscribe the same way you subscribe, except with -=.

And I don't think the "duh" was to insult you, just something that we've all done at one point in our careers. Sometimes many more times than once :P
Was This Post Helpful? 0
  • +
  • -

#5 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

Re: FileSystemWatcher

Posted 23 August 2011 - 08:08 PM

This is why I stopped coming to this forum lol
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4574
  • View blog
  • Posts: 8,013
  • Joined: 08-June 10

Re: FileSystemWatcher

Posted 23 August 2011 - 08:11 PM

Why, because you get useful and on-point answers, with a little color commentary? Glad you came back six months later to tell us.
Was This Post Helpful? 2
  • +
  • -

#7 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

Re: FileSystemWatcher

Posted 23 August 2011 - 08:13 PM

no because of the dicker y and star trek outfits
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4574
  • View blog
  • Posts: 8,013
  • Joined: 08-June 10

Re: FileSystemWatcher

Posted 23 August 2011 - 08:15 PM

Fine. If you want to complain, take it up with the admins. Threads in the programming forums are for help only. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1