Try another handler:
private void OperationsOnOnTraverseEvent( string status )
{
ResultsLabel.Invoke( (MethodInvoker)( ( ) => ResultsLabel.Text = status ) );
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I test for read All files in C:\ .I try this code. but nothing happen.
somebody show me why.
In Form code is;
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;
using System.IO;
using System.Threading;
using Learning_8;
namespace Learning_8
{
public partial class Form1 : Form
{
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
public Form1()
{
InitializeComponent();
Operations.OnTraverseEvent += OperationsOnOnTraverseEvent;
}
private void OperationsOnOnTraverseEvent(string status)
{
if(ResultsLabel.InvokeRequired)
{
ResultsLabel.Invoke((MethodInvoker)(() =>
ResultsLabel.Text = status));
ResultsLabel.Refresh();
};
}
private async void Button1_Click(object sender, EventArgs e)
{
if (_cancellationTokenSource.IsCancellationRequested)
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
}
var directoryInfo = new DirectoryInfo("D:\\");
try
{
await Operations.RecursiveFolders(
directoryInfo,
new[] { "*.txt" },
_cancellationTokenSource.Token);
if (Operations.Cancelled)
{
MessageBox.Show(@"You cancelled the operation");
}
else
{
MessageBox.Show(@"Done");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button2_Click(object sender, EventArgs e)
{
_cancellationTokenSource.Cancel();
}
}
}
public static async Task RecursiveFolders(DirectoryInfo directoryInfo, string[] excludeFileExtensions, CancellationToken ct)
{
if (!directoryInfo.Exists)
{
if (OnTraverseEvent != null)
OnTraverseEvent("Nothing to process");
return;
}
if (!excludeFileExtensions.Any(directoryInfo.FullName.Contains))
{
await Task.Delay(1);
if (OnTraverseEvent != null)
OnTraverseEvent(directoryInfo.FullName);
}
else
{
if (OnTraverseExcludeFolderEvent != null)
OnTraverseExcludeFolderEvent(directoryInfo.FullName);
}
DirectoryInfo folder = null;
try
{
await Task.Run(async () =>
{
foreach (DirectoryInfo dir in directoryInfo.EnumerateDirectories())
{
folder = dir;
if ((folder.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden || (folder.Attributes & FileAttributes.System) == FileAttributes.System || (folder.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
{
if (OnTraverseExcludeFolderEvent != null)
OnTraverseExcludeFolderEvent($"* {folder.FullName}");
continue;
}
if (!Cancelled)
{
await Task.Delay(1);
await RecursiveFolders(folder, excludeFileExtensions, ct);
}
else
{
return;
}
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
}
}
});
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
Cancelled = true;
}
else if (ex is UnauthorizedAccessException)
{
if (UnauthorizedAccessExceptionEvent != null)
UnauthorizedAccessExceptionEvent($"Access denied '{ex.Message}'");
}
else
{
if (OnExceptionEvent != null)
OnExceptionEvent(ex);
}
}
}
public static void RecursiveFolders(string path, int indentLevel)
{
try
{
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
{
foreach (string folder in Directory.GetDirectories(path))
{
Debug.WriteLine($"{new string(' ', indentLevel)}{System.IO.Path.GetFileName(folder)}");
RecursiveFolders(folder, indentLevel + 2);
}
}
}
catch (UnauthorizedAccessException unauthorized)
{
Debug.WriteLine($"{unauthorized.Message}");
}
}
}
} Thank.
Try another handler:
private void OperationsOnOnTraverseEvent( string status )
{
ResultsLabel.Invoke( (MethodInvoker)( ( ) => ResultsLabel.Text = status ) );
}