Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7725649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:59:32+00:00 2026-06-01T04:59:32+00:00

Is it possible to implement Delegate methods for FileSystemWatcher? I have a delegate method

  • 0

Is it possible to implement Delegate methods for FileSystemWatcher?

I have a delegate method named myDelegate which I want to call inside the timer function OnElapsedTimer, I tried calling myDelegate to invoke, OnDeleted, but the second argument is giving an compilation error. How can I pass FileSystemEventArgs in the delegate method?

using System;
using System.IO;
using System.Security.Permissions;
using System.Timers;

public class Watcher
{

  public delegate void myDelegae(object source, FileSystemEventArgs e);

  public static void Main()
  {        
    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new ElapsedEventHandler(OnElapsedTimer);
    aTimer.Interval = 5000;
    aTimer.Enabled = true;
    Run();
    while (Console.Read() != 'q') ;
  }

  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  public static void Run()
  {
    string path = @"C:\\File";
    FileSystemWatcher watcher = new FileSystemWatcher(path);
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName |  NotifyFilters.Attributes;

    watcher.NotifyFilter = NotifyFilters.DirectoryName; 

    // Add event handlers.
    watcher.Deleted += new FileSystemEventHandler(OnDeleted);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

  }

  private static void OnDeleted(object source, FileSystemEventArgs e)
  {
      Console.WriteLine("File: {0}", e.FullPath);
  }

  private static void OnElapsedTimer(object source, ElapsedEventArgs e)
  {
    Console.WriteLine("Hello World!");
    if (!Directory.Exists("C:\\File"))
    {
      //Invoke  OnDeleted here
        var fseArgs = new FileSystemEventArgs(WatcherChangeTypes.Deleted, @"C:\",  @"File");
        OnDeleted("C:\\File",fseArgs);
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:59:33+00:00Added an answer on June 1, 2026 at 4:59 am

    I’m assuming that you’re trying to call OnDelete(e) inside OnElapsedTimer(), which of course fails because that e is of type ElapsedEventArgs, and not FileSystemEventArgs.

    The way to make this work is to create your own FileSystemEventArgs object, and pass that to OnDelete(). There’s nothing special about the class, it simply stores information relevant to the operation that was performed. In this case, you know what that operation is: The directory called “C:\File” has been deleted:

    var fseArgs = new FileSystemEventArgs(WatcherChangeTypes.Deleted, @"C:\", @"File");
    OnDeleted(fseArgs);
    

    Note that you’re almost certainly better off just listening in the OnDelete() handler itself; why reproduce functionality that already works?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a method which takes two Action A1 and Action A2
Is it possible to implement associative arrays as Selectors and Values. I have an
I've possible got to implement SAML for a couple of sites we have and
Possible Duplicates: How would I implement stackoverflow’s hovering dialogs? i want a small box
is it possible to create a generic method in interfaces? say I want to
In C#, is it possible to have an object that has multiple method signatures
I want to emit a method that returns a Func<>. Inside this method I
Hi I have manged to implement didSelectViewController in the app delegate but is it
I have a method called -showMoreTools: which is: - (IBAction) showMoreTools:(id)sender { UIActionSheet *popupQuery
I basically want to implement a Label and have it constantly showing information to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.