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

  • Home
  • SEARCH
  • 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 8962571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:09:24+00:00 2026-06-15T16:09:24+00:00

I am new to Rx and looking to use it in my current project.

  • 0

I am new to Rx and looking to use it in my current project. I am trying to implement a file watcher system. Atleast for now I am only interested in file Creation event. But I am getting “Value cannot be null error message” when trying to run below code.
Please could someone help me out with below code.

class Program
{
    static void Main(string[] args)
    {
        IDisposable writer = new FileSystemObservable(@"D:\Code\Rx\Dropbox\", "*.*", false)
                            .CreatedFiles
                            .Where(x => (new FileInfo(x.FullPath)).Length > 0)
                            .Select(x => x.Name)
                            .Subscribe(Console.WriteLine);
        Console.ReadLine();
    }
}


class FileSystemObservable
{
    private readonly FileSystemWatcher fileSystemWatcher;

    public FileSystemObservable(string directory, string filter, bool includeSubdirectories)
    {
        fileSystemWatcher = new FileSystemWatcher(directory, filter)
            {
                EnableRaisingEvents = true,
                IncludeSubdirectories = includeSubdirectories
            };

        CreatedFiles = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                (h => fileSystemWatcher.Created += h,
                h => fileSystemWatcher.Created -= h)
                .Select(x => new { x.EventArgs }) as IObservable<FileSystemEventArgs>;

        Errors = Observable.FromEventPattern<ErrorEventHandler, ErrorEventArgs>
                (h => fileSystemWatcher.Error += h,
                h => fileSystemWatcher.Error -= h)
                .Select(x => new { x.EventArgs }) as IObservable<ErrorEventArgs>;
    }

    public IObservable<ErrorEventArgs> Errors { get; private set; }

    public IObservable<FileSystemEventArgs> CreatedFiles { get; private set; }
}
  • 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-15T16:09:25+00:00Added an answer on June 15, 2026 at 4:09 pm

    The result of the

    Select(x => new { x.EventArgs }) as IObservable<ErrorEventArgs> 
    

    and

    .Select(x => new { x.EventArgs }) as IObservable<FileSystemEventArgs>;
    

    lines will always return null.

    The type of Select(x => new { x.EventArgs }) is IObservable<'a> where 'a is some anonymous type.

    You should instead use:

    CreatedFiles = 
        Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
            h => fileSystemWatcher.Created += h,
            h => fileSystemWatcher.Created -= h)
        .Select(x => x.EventArgs);
    
    Errors = 
        Observable.FromEventPattern<ErrorEventHandler, ErrorEventArgs>(
            h => fileSystemWatcher.Error += h,
            h => fileSystemWatcher.Error -= h)
        .Select(x => x.EventArgs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning to start on a new project and am looking at the current
I am relatively new to Android, but I am looking to use an interactive
I'm looking for a framework I can use in my new webproject. The main
I'm looking to rewrite my git repo to use a new branching model I
I'm looking to start a new project to work on in my free time
I started to implement some new html5 features (standards, nothing fancy) in my project.
I am trying to use Ember.js in my Rails project. I created scaffold of
I am about to start a new project that will use the Play! 2
Finally reached data migration part of my Project and now trying to move data
I've been trying to use some code like the following to read current sound

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.