I have method to call from OnCreated_.How to call two string in one time.

MIPAKTEH_1 385 Reputation points
2024-11-20T12:47:13.17+00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dir_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileSystemWatcher watcher = new FileSystemWatcher(@"c:\\")
            {
                NotifyFilter = NotifyFilters.Attributes
                | NotifyFilters.DirectoryName
                | NotifyFilters.FileName
                | NotifyFilters.LastWrite
                | NotifyFilters.Size,
                Filter = "*.*",  // Monitor all file types
                IncludeSubdirectories = true,
                EnableRaisingEvents = true
            };
            watcher.Created += OnCreated_;

        }

        void OnCreated_(object sender, FileSystemEventArgs e)
        {


            try
            {
                // How to call getFiles.
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }
        }
        void getFiles(string directory)
        {
            string[] files = Directory.GetFiles(directory);
            string[] directories = Directory.GetDirectories(directory);

            foreach (string file in files)
            {
                getFiles(file);
                listBox1.Invoke((Action)(() => listBox1.Items.Add(file)));
                File.Delete(file);
            }

            foreach (string subDirectory in directories)
            {
                // Call the same method on each directory.
                getFiles(subDirectory);
                listBox2.Invoke((Action)(() => listBox2.Items.Add(subDirectory)));
                File.Delete(subDirectory);
            }


        }

    }
}

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.
11,053 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 46,801 Reputation points Microsoft Vendor
    2024-11-21T02:12:05.7233333+00:00

    Hi @MIPAKTEH_1 , Welcome to Microsoft Q&A,

    You need to adjust your logic to ensure that you get the path to the file or directory from the FileSystemWatcher and pass it as a parameter to the getFiles method.

    This is a one-time effect that occurs when you create a new file in the target folder.

    void OnCreated_(object sender, FileSystemEventArgs e)
    {
        try
        {
            // e.FullPath is the full path of the created file or directory
            string createdPath = e.FullPath;
    
            if (Directory.Exists(createdPath))
            {
                // If it is a directory
                getFiles(createdPath);
            }
            else if (File.Exists(createdPath))
            {
                // If it is a file, get the directory
                string directory = Path.GetDirectoryName(createdPath);
                getFiles(directory);
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine($"Error: {exception.Message}");
        }
    }
    
    // Recursively traverse the directory and update the ListBox
    void getFiles(string directory)
    {
        try
        {
            // Get files and subdirectories in the directory
            string[] files = Directory.GetFiles(directory);
            string[] directories = Directory.GetDirectories(directory);
    
            // Process files
            foreach (string file in files)
            {
                listBox1.Invoke((Action)(() => listBox1.Items.Add(file))); // Update ListBox
            }
    
            // Process subdirectories
            foreach (string subDirectory in directories)
            {
                listBox2.Invoke((Action)(() => listBox2.Items.Add(subDirectory))); // Update ListBox
                getFiles(subDirectory); // Recursive call
            }
        }
        catch (UnauthorizedAccessException)
        {
            Debug.WriteLine($"Access denied to: {directory}");
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"Error processing directory: {directory}, {ex.Message}");
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.