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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:20:53+00:00 2026-06-04T07:20:53+00:00

I have listBox1 and listBox2 which are loaded with some files from a directory.

  • 0

I have listBox1 and listBox2 which are loaded with some files from a directory. Initially when I load the form I disabled buttons for the listBox1 and listBox2.

When there is no file in the directory I want to disable the button1 for listBox1 and button2 listBox2.

But I want to enable the buttons of the corresponding listBox1 and listBox2 if there is a single file in the directory and if there is any change in the files.

How do i perform this using FileSystemWatcher ??? I need some idea to perform this.

private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
  DirectoryInfo dinfo = new DirectoryInfo(Folder);
  FileInfo[] Files = dinfo.GetFiles(FileType);
  foreach (FileInfo file in Files)
  {
    lsb.Items.Add(file);
  }
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  FileInfo file = (FileInfo)listBox1.SelectedItem;
  string path = file.FullName;
  DisplayFile(path);
}

private void button1_Click(object sender, EventArgs e)
{

}


private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{

}

I have initialized a method here, from here how can i proceed???

public void filesystemwatcher()
{
  FileSystemWatcher watcher = new FileSystemWatcher();
  watcher.Path = @"C:\LoadFiles\";                      
}
  • 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-04T07:20:54+00:00Added an answer on June 4, 2026 at 7:20 am

    You can use a BindingList with the FileSystemWatcher:

    BindingList<string> _Files = new BindingList<string>();
    
    public Form1() {
      InitializeComponent();
    
      _Files.ListChanged += new ListChangedEventHandler(Files_ListChanged);
      fileSystemWatcher1.Deleted += fileSystemWatcher1_Deleted;
      fileSystemWatcher1.Created += fileSystemWatcher1_Created;
      fileSystemWatcher1.Renamed += fileSystemWatcher1_Renamed;
      fileSystemWatcher1.Path = @"C:\TestLoadFiles\";
    
      foreach (string f in Directory.GetFiles(fileSystemWatcher1.Path)) {
        _Files.Add(Path.GetFileName(f));
      }
      listBox1.DataSource = _Files;
    }
    
    private void Files_ListChanged(object sender, ListChangedEventArgs e) {
      button1.Enabled = (_Files.Count > 0);
    }
    
    private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e) {
      _Files[_Files.IndexOf(e.OldName)] = e.Name;
    }
    
    private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e) {
      _Files.Remove(e.Name);
    }
    
    private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) {
      _Files.Add(e.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main form, which is invisible and at some point creates a
I have a Listbox which work with specific extension files. If i choose another
I have a listbox in which i have to give minimum 2 files for
I have a WP7 which read a XML file , take some elements and
I have a ListBox & it has some files. I have 2 Panels in
I have a class ExpanderItems which gets loaded during runtime and a list of
I have a Panorama control, which has an ExpanderView Item (from Silverlight toolkit). My
I have a listbox which acts as a list of items. If you click
I have a ListBox which uses a DataTemplate to render databound items. The XAML
[Original] I have a ListBox which has its ItemsSource (this is done in the

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.