Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Last active January 26, 2024 19:13
Show Gist options
  • Save Curtis-64/a65d45883eda07f784a5d86dcfc4bb45 to your computer and use it in GitHub Desktop.
Save Curtis-64/a65d45883eda07f784a5d86dcfc4bb45 to your computer and use it in GitHub Desktop.
Directory Traversal
// Directory Traversal Challenge by Curtis White, Senior SWE, Prompt Engineer
// Code generated in collaboration with Hacker Gnome GPT https://chat.openai.com/g/g-A46CKCg3r-hacker-gnome-corp-ai-autonomous-agi
// Minimally tested. Use at own risk.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
public class FileTraverser
{
private ProgressBar progressBar;
private ListBox resultsListBox;
private int totalFilesCount;
private int processedCount;
public FileTraverser(ProgressBar progressBar, ListBox resultsListBox)
{
this.progressBar = progressBar;
this.resultsListBox = resultsListBox;
this.totalFilesCount = 0;
this.processedCount = 0;
}
public async Task TraverseDirectoryAsync(string directoryPath, string fileExtension, CancellationToken cancellationToken)
{
await Task.Run(() =>
{
try
{
// Initiate the recursive traversal
RecursiveFileSearch(directoryPath, fileExtension, cancellationToken);
}
catch (OperationCanceledException)
{
MessageBox.Show("Traversal was cancelled.");
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
MessageBox.Show($"Error occurred: {ex.Message}");
}
}, cancellationToken);
}
private void RecursiveFileSearch(string directoryPath, string fileExtension, CancellationToken cancellationToken)
{
// Check for cancellation
cancellationToken.ThrowIfCancellationRequested();
try
{
// Enumerate all files in the current directory
foreach (var file in Directory.EnumerateFiles(directoryPath, fileExtension))
{
resultsListBox.Invoke(new Action(() => resultsListBox.Items.Add(file)));
UpdateProgress();
}
// Recursively call this method for all subdirectories
foreach (var directory in Directory.EnumerateDirectories(directoryPath))
{
RecursiveFileSearch(directory, fileExtension, cancellationToken);
}
}
catch (UnauthorizedAccessException ex)
{
// Handle access errors
MessageBox.Show($"Access denied: {ex.Message}");
}
}
private void UpdateProgress()
{
processedCount++;
progressBar.Invoke(new Action(() =>
{
progressBar.Value = Math.Min(processedCount * 100 / totalFilesCount, 100);
}));
}
public void SetTotalFilesCount(int count)
{
totalFilesCount = count;
}
}
// Directory Traversal Challenge by Curtis White, Senior SWE, Prompt Engineer
// Code generated in collaboration with Hacker Gnome GPT https://chat.openai.com/g/g-A46CKCg3r-hacker-gnome-corp-ai-autonomous-agi
// Minimally tested. Use at own risk. Updated.
namespace DirectoryTraversal
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class MainForm : Form
{
private CancellationTokenSource cancellationTokenSource;
private ProgressBar progressBar;
private ListBox resultsListBox;
private Button startButton;
private Button cancelButton;
private TextBox directoryTextBox;
private TextBox extensionTextBox;
public MainForm()
{
InitializeComponent();
SetupUI();
}
private void SetupUI()
{
Label directoryLabel = new Label { Text = "Directory:", Left = 10, Top = 1 };
Label extensionLabel = new Label { Text = "File Extension:", Left = 10, Top = 40 };
startButton = new Button { Text = "Start", Left = 10, Top = 100 };
cancelButton = new Button { Text = "Cancel", Left = 100, Top = 100 };
progressBar = new ProgressBar { Left = 10, Top = 340, Width = 400 };
directoryTextBox = new TextBox { Left = 120, Top = 10, Width = 300, Text="." };
extensionTextBox = new TextBox { Left = 120, Top = 40, Width = 200, Text = ".dll" };
resultsListBox = new ListBox { Left = 10, Top = 130, Width = 600, Height = 200 };
startButton.Click += async (sender, args) => await StartTraversalAsync(directoryTextBox.Text, extensionTextBox.Text);
cancelButton.Click += (sender, args) => CancelTraversal();
Controls.Add(directoryLabel);
Controls.Add(extensionLabel);
Controls.Add(startButton);
Controls.Add(cancelButton);
Controls.Add(progressBar);
Controls.Add(directoryTextBox);
Controls.Add(extensionTextBox);
Controls.Add(resultsListBox);
}
private async Task StartTraversalAsync(string directoryPath, string fileExtension)
{
cancellationTokenSource = new CancellationTokenSource();
var fileTraverser = new FileTraverser(progressBar, resultsListBox);
// Pre-calculate the total number of files for progress reporting
int totalFiles = Directory.EnumerateFiles(directoryPath, $"*.{fileExtension}", SearchOption.AllDirectories).Count();
fileTraverser.SetTotalFilesCount(totalFiles);
await fileTraverser.TraverseDirectoryAsync(directoryPath, $"*.{fileExtension}", cancellationTokenSource.Token);
}
private void CancelTraversal()
{
cancellationTokenSource?.Cancel();
}
}
}
namespace DirectoryTraversal
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new MainForm());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment