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 7763751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:44:16+00:00 2026-06-01T14:44:16+00:00

I need a custom event that should raise if a new/existing file in a

  • 0

I need a custom event that should raise if a new/existing file in a specific directory was found/created. To check if a new File was created I use the SystemFileWatcher, which works fine. To check if some files exist at program start I wrote some lines and that works to.

I use this class for event args:

public class FileDetectEventArgs : EventArgs
{
    public String Source { get; set; }
    public String Destination { get; set; }
    public String FullName { get; set; }

    public FileDetectEventArgs(String source, String destination, String fullName)
    {
        this.Source = source;
        this.Destination = destination;
        this.FullName = fullName;
    }
}

If the SystemFileWatcher raise a FileCreated Event I use these lines of code:

public void onFileCreated(object sender, FileSystemEventArgs e)
{
    // check if file exist
    if (File.Exists(e.FullPath))
    {
        OnNewFileDetect(new FileDetectEventArgs(source, destination, e.FullPath));  
    }
}

And if a file exist I try to raise the event in that way:

public void checkExistingFiles(String source, String filter)
    {
        DirectoryInfo di = new DirectoryInfo(source);
        FileInfo[] fileInfos = di.GetFiles();
        String fileFilter = filter.Substring(filter.LastIndexOf('.'));       

        foreach (FileInfo fi in fileInfos)
        {
            if (fi.Extension.Equals(fileFilter))
            {
                OnNewFileDetect(new FileDetectEventArgs(source, destination, fi.FullName));                   
            }
        }
    }

Here is the OnNewFileDetect-Event:

protected void OnNewFileDetect(FileDetectEventArgs e)
    {
        if (OnNewFileDetectEvent != null)
        {
            OnNewFileDetectEvent(this, e);
        }
    }

The problem is, if the onFileCreated-Event raises my OnNewFileDetect-Event all works fine. But if the checkExistingFiles found some existing files and try to raise OnNewFileDetect-Event nothing happen. I found out that the OnNewFileDetectEvent-Object is null and so nothing happen. But why it’s not null if the onFileCreated-Event is fired?

  • 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-01T14:44:17+00:00Added an answer on June 1, 2026 at 2:44 pm

    But why its not null if the onFileCreated-Event is fired?

    This event will be null until something subscribes to the event.


    On a side note, I’d consider switching to a better pattern for raising the event, as well as using standard C#/.NET naming here. This would be more like:

    // The event...
    public EventHandler<FileDetectedEventArgs> NewFileDetected;
    
    // Note the naming
    protected void OnNewFileDetected(FileDetectedEventArgs e)
    {
        // Note this pattern for thread safety...
        EventHandler<FileDetectedEventArgs> handler = this.NewFileDetected; 
        if (handler != null)
        {
            handler(this, e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement classes to support a custom event capable of passing varying
I making a custom button, and I need to add a PreviewKeyDown event, whenever
I need create custom dialog and put JPanel into it. Is it possible?
I need a custom widget height. I tried using this Integer.toString(yourWidget.getElement().getOffsetHeight()) but, If I
So I need some custom colors on a layout, but I'm looking for a
I'm trying to modify the Django Admin interface. I need a custom type of
I need to make custom text field which will format numeric values according to
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
I need to write custom ord and chr functions. I'm writing a little program
I have Forms authentication and I need a custom object to be stored in

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.