A community member has associated this post with a similar question:
Put all files from Filesystemwacther to virtual Mode ListView

Only moderators can edit this content.

How to show files from filesystemwatcher into ListView Virtual Mode using Class

MiPakTeh 1,476 Reputation points
2021-03-31T01:27:53.387+00:00

I test the code bellow but the listview show Nothing.Can somebody tell me and what the actually coding.
Thank.

1.Class

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.IO.Path;


namespace C2
{
    public class FileOperations : FileSystemWatcher
    {

        /// <summary>
        /// Path to check for new files
        /// </summary>
        public string MonitorPath { get; set; }

        public FileOperations(string monitorPath)
        {
            MonitorPath = monitorPath;

            Created += OnCreated;
            Error += OnError;

            Path = MonitorPath;

            EnableRaisingEvents = true;

            NotifyFilter = NotifyFilters.Attributes
                           | NotifyFilters.CreationTime
                           | NotifyFilters.DirectoryName
                           | NotifyFilters.FileName
                           | NotifyFilters.LastAccess
                           | NotifyFilters.LastWrite
                           | NotifyFilters.Security
                           | NotifyFilters.Size;
        }

        public void OnCreated(object sender, FileSystemEventArgs e)
        {
            try
            {
            MonitorPath = (e.FullPath);
            }

            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }
        }

        public void Start()
        {
            EnableRaisingEvents = true;
        }

        public void Stop()
        {
            EnableRaisingEvents = false;
        }

        private static void OnError(object sender, ErrorEventArgs e) => DisplayException(e.GetException());
        /// <summary>
        /// For debug purposes
        /// For a production environment write to a log file
        /// </summary>
        /// <param name="ex"></param>
        private static void DisplayException(Exception ex)
        {
            if (ex != null)
            {
                Debug.WriteLine($"Message: {ex.Message}");
                Debug.WriteLine("Stacktrace:");
                Debug.WriteLine(ex.StackTrace);
                Debug.WriteLine("");

                DisplayException(ex.InnerException);
            }
        }


    }
}
  1. Form Code using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms; namespace C2
    {
    public partial class Form1 : Form
    {
        private readonly FileOperations _fileOperations = new FileOperations(@"C:\");
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
            listView1.VirtualMode = true;
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.Columns.Add("Filename", 600);
            listView1.VirtualListSize = _fileOperations.MonitorPath.Count();
        }
        void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            var item = new ListViewItem();
            if (_fileOperations.MonitorPath.Count() > 0)
            {
                item.Text = _fileOperations.MonitorPath[e.ItemIndex].ToString();
            }
            e.Item = item;
    
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _fileOperations.Start();
    
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            _fileOperations.Stop();
        }
    }
    
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,224 questions
0 comments No comments
{count} votes