Counter inside a recursive function - c#

I need to count the number of files deleted in this recursive function. Since it's recursive I cannot use if statements, and C# does not support global variables. Any alternatives?
static void DirSearch(string path)
{
try
{
foreach (string dirPath in Directory.GetDirectories(path))
{
foreach (string filePath in Directory.GetFiles(dirPath))
{
string filename = Path.GetFileName(filePath);
if (filename.Equals("desktop.txt"))
{
File.Delete(filePath);
//count++
}
Console.WriteLine(filePath); // print files
}
Console.WriteLine(dirPath); // print directories
DirSearch(dirPath);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}

One way is to pass in something for it to count into. I'd do this using ref, for example:
static void DirSearch(string path, ref int count)
{
try
{
foreach (string dirPath in Directory.GetDirectories(path))
{
foreach (string filePath in Directory.GetFiles(dirPath))
{
string filename = Path.GetFileName(filePath);
if (filename.Equals("desktop.txt"))
{
File.Delete(filePath);
count++
}
Console.WriteLine(filePath); // print files
}
Console.WriteLine(dirPath); // print directories
DirSearch(dirPath,ref count);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Then call it:
int count = 0;
DirSearch(#"C:\SomePath",ref count);
Then you can use count as normal as you had commented out in your code.

Try a recursive count as follows. So DirSearch returns the count of deleted files.
static int DirSearch(string path)
{
int count = 0;
try
{
foreach (string dirPath in Directory.GetDirectories(path))
{
foreach (string filePath in Directory.GetFiles(dirPath))
{
string filename = Path.GetFileName(filePath);
if (filename.Equals("desktop.txt"))
{
File.Delete(filePath);
count++;
}
Console.WriteLine(filePath); // print files
}
Console.WriteLine(dirPath); // print directories
count += DirSearch(dirPath);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
return count;
}

Without a ref variable (so you don't need to pass something that is correctly initialized):
static int DirSearch(string path)
{
try
{
int count = 0;
foreach (string dirPath in Directory.GetDirectories(path))
{
foreach (string filePath in Directory.GetFiles(dirPath))
{
string filename = Path.GetFileName(filePath);
if (filename.Equals("desktop.txt"))
{
File.Delete(filePath);
count++;
}
Console.WriteLine(filePath); // print files
}
Console.WriteLine(dirPath); // print directories
count += DirSearch(dirPath);
}
return count;
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}

Did You also consider a non-recursive solution like presented here? https://stackoverflow.com/a/929418/2979680

Related

How to read and show content from multiple text file?

As the question, how to show content from multiple text file? I would like to read all the content from the path show in the console with 1 press button and store into a variable. Currently it only can read 1 by one. I'm self learner and begineer for the C# currently.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
ListFileInDirectory(#"C:\Users\liewm\Desktop\SampleFile");
Console.WriteLine("Press enter to continue");
Console.Read();
}
private static void ListFileInDirectory(string workingDirectory)
{
string[] filePaths = Directory.GetFiles(workingDirectory);
String line;
foreach (string filePath in filePaths)
{
Console.WriteLine(filePath);
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(filePath);
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line= sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
}
You can try like this
private static void ListFileInDirectory(string workingDirectory)
{
string[] filePaths = Directory.GetFiles(workingDirectory);
String line;
foreach (string filePath in filePaths)
{
Console.WriteLine(filePath);
try
{
//it returns string[]
var allText = System.IO.File.ReadAllLines(filePath);//opens a text file, reads all text and closes the text file.
foreach(var str in allText)
{
//Do what you want.
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
static void Main(string[] args)
{
//ListFileInDirectory(#"C:\Users\albto\Documents\_projetos\MultiplesFiles\MultiplesFiles\_arquivos");
ListFileInDirectory(#"C:\Users\liewm\Desktop\L907C524_1x");
Console.WriteLine("Press enter to continue");
Console.Read();
}
private static void ListFileInDirectory(string workingDirectory)
{
string[] filePaths = Directory.GetFiles(workingDirectory);
List<string> allLines = new List<string>();
foreach (string filePath in filePaths)
{
try
{
//stores the files address
allLines.Add(filePath);
//stores file lines
allLines.AddRange(System.IO.File.ReadAllLines(filePath));
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
//shows all stored lines
allLines.ForEach(Console.WriteLine);
Console.ReadLine();
}

system io file not found exception/ error handling

I'm working in C# and I try to do a program that get some infoes the files in a Directory. I made it but i have a problem with the error Handling. When the program runs and for example I give just random numbers to list file infoes i get this error message:
"System.IO.DirectoryNotFoundException: "'Could not find a part of the path 'C:\Temp\first_project\first_project\bin\Debug\12345'.'"
Please someone help me to do the error handling.
Thank you in advance.
using System;
using System.IO;
class Test
{
static void Main(string[] args)
{
Console.WriteLine("Please :");
string hely = Console.ReadLine();
string[] __file = Directory.GetFiles(hely);
string[] __dir = Directory.GetDirectories(hely);
foreach (string i in __file)
{
FileInfo fajl = new FileInfo(i);
Console.WriteLine("{0},{1},{2}", fajl.Name, fajl.Extension, fajl.LastWriteTime.ToString());
}
foreach (string i in __dir)
{
DirectoryInfo _file = new DirectoryInfo(i);
Console.WriteLine("{0},{1},{2}", _file.Name, _file.Extension, _file.LastWriteTime.ToString());
}
Console.ReadKey();
}
}
You should check existence of a path with
System.IO.Directory.Exists(directory)
and of a file with
System.IO.File.Exists(filePath)
Then, you need to take the try-catch block inside the for-loop, to catch any possible exceptions that occur because of insufficient rights/permissions.
e.g.
foreach (string i in __file)
{
try
{
FileInfo fajl = new FileInfo(i);
Console.WriteLine("{0},{1},{2}", fajl.Name, fajl.Extension, fajl.LastWriteTime.ToString());
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
}
You could also create two try-catch blocks - depends on what you want to do.
try
{
foreach (string i in __file)
{
try
{
FileInfo fajl = new FileInfo(i);
Console.WriteLine("{0},{1},{2}", fajl.Name, fajl.Extension, fajl.LastWriteTime.ToString());
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
}
}
catch (System.Exception exLoop)
{
System.Console.WriteLine(exLoop.Message);
throw;
}
Note that in your example, you should first check if the directory "hely" exists:
if (!System.IO.Directory.Exists(hely))
{
System.Console.Error.WriteLine("Directory \"{0}\" does not exist.", hely);
System.Environment.Exit(1);
// or: return;
}
Since exception handling is usually very slow, I would however recommend that you check for the existence of the file/directory explicitly. It would also be a good idea to do so for the file/directory-listing & read-access rights for the respective user. But even if you do so, keep the try-catch, because there might be cases where your program suddenly fails - e.g. when a removable storage is forcefully removed.
Use try catch
using System;
using System.IO;
class Test
{
static void Main(string[] args)
{
Console.WriteLine("Please :");
string hely = Console.ReadLine();
try
{
string[] __file = Directory.GetFiles(hely);
string[] __dir = Directory.GetDirectories(hely);
foreach (string i in __file)
{
FileInfo fajl = new FileInfo(i);
Console.WriteLine("{0},{1},{2}", fajl.Name, fajl.Extension, fajl.LastWriteTime.ToString());
}
foreach (string i in __dir)
{
DirectoryInfo _file = new DirectoryInfo(i);
Console.WriteLine("{0},{1},{2}", _file.Name, _file.Extension, _file.LastWriteTime.ToString());
}
}
catch(System.IO.DirectoryNotFoundException ex)
{
Console.WriteLine("Directory not found");
}
Console.ReadKey();
}
}
You can check if the file exists
foreach (string i in __file)
{
if (File.Exists(i))
{
FileInfo fajl = new FileInfo(i);
Console.WriteLine("{0},{1},{2}", fajl.Name, fajl.Extension, fajl.LastWriteTime.ToString());
}
}
RTFM?
Read Directory.GetFiles method
It says that you will get the DirectoryNotfound exception if the specified path is not found. Obviously folder 'C:\Temp\first_project\first_project\bin\Debug\12345' does not exist.
Proper code would be:
string hely = ...
try
{
string[] files = Directory.GetFiles(hely);
ProcessFiles(files);
}
catch (DirectoryNotFoundException exc)
{
Console.WriteLine(exc.Message);
}
If you don't know how to react on exceptions read MSDN about exception handling

c# - How to multithreading Iterate Through a Directory Tree, in folder 1000000 files

I need to save in database file Name and Size in byte from folder and all subfolder.
In this folder lay 1 000 000 files.
And when I use example from msdn it works 4 days, that very slowly.
static void Main(string[] args)
{
string pdxPathDocFiles = System.Configuration.ConfigurationManager.AppSettings["PDX_PathDocFiles"] as string;
if (string.IsNullOrEmpty(pdxPathDocFiles))
{
Console.WriteLine("In the configuration file is missing the path to the root directory - PDX_PathDocFiles.");
}
else
{
if (!Directory.Exists(pdxPathDocFiles))
{
Console.WriteLine("Directory not found");
}
else
{
try
{
Console.WriteLine("rootPath: " + pdxPathDocFiles);
PayDox_EPD19_T20_RGMEntities db = new PayDox_EPD19_T20_RGMEntities();
System.IO.DirectoryInfo rootDir = new DirectoryInfo(pdxPathDocFiles);
db.FileDBRecord.RemoveRange(db.FileDBRecord);
WalkDirectoryTree(rootDir, rootDir.ToString(), db);
db.SaveChanges();
}
catch (Exception)
{
Console.WriteLine("Failed to connect to the database");
}
Console.WriteLine("All ok");
}
}
Console.WriteLine("Bye, Good Day.");
}
static void WalkDirectoryTree(System.IO.DirectoryInfo root, string rootDir, PayDox_EPD19_T20_RGMEntities db)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
try
{
files = root.GetFiles("*.*");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
db.FileDBRecord.Add(new FileDBRecord { FileName = fi.FullName.Replace(rootDir, ""), FileSize = fi.Length });
}
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
WalkDirectoryTree(dirInfo, rootDir, db);
}
}
db.SaveChanges();
}
When I try another way, it throw-out with exception stack overflow exception.
static void Main(string[] args)
{
string pdxPathDocFiles = System.Configuration.ConfigurationManager.AppSettings["PDX_PathDocFiles"] as string;
if (string.IsNullOrEmpty(pdxPathDocFiles))
{
Console.WriteLine("In the configuration file is missing the path to the root directory - PDX_PathDocFiles.");
}
else
{
if (!Directory.Exists(pdxPathDocFiles))
{
Console.WriteLine("Directory not found");
}
else
{
try
{
Console.WriteLine("rootPath: " + pdxPathDocFiles);
PayDox_EPD19_T20_RGMEntities db = new PayDox_EPD19_T20_RGMEntities();
db.FileDBRecord.RemoveRange(db.FileDBRecord);
db.SaveChanges();
Console.WriteLine("Remove data from table");
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo rootDir2 = new DirectoryInfo(pdxPathDocFiles);
try
{
files = rootDir2.GetFiles("*.*", SearchOption.AllDirectories);
Console.WriteLine("Reed {0} fileName", files.Length);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("You do not have permission to access one or more folders in this directory tree.");
Console.WriteLine(ex.Message);
return;
}
db.FileDBRecord.AddRange(files.Select(x => new FileDBRecord { FileName = x.FullName.Replace(pdxPathDocFiles, ""), FileSize = x.Length }));
db.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("All ok");
}
}
Console.WriteLine("Bye, Good Day.");
}
How make program faster, maybe add multithreading?
For starters, your code isn't async. Break this out into a separate class and make the methods async. This allows the thread to be used while waiting for an IO operation. Anytime your calling the Database or File system use async equivalent methods.
The second thing I would do is try to make is so each transaction is atomic. If you doing something x amount of times, write the program in such a way that each x time can be done is isolation. Once that is done you can run these is parallel by creating a new Task (Task.Run).
Once those 2 are done and the task is still taking a while, look into TPL Dataflow. That can buffer requests for you to optimize your process.
I improved first example from msdn, by adding there TPL library.
Now it working 4 hour, not 4 days.
static void Main(string[] args)
{
string pdxPathDocFiles = System.Configuration.ConfigurationManager.AppSettings["PDX_PathDocFiles"] as string;
if (string.IsNullOrEmpty(pdxPathDocFiles))
{
Console.WriteLine("In the configuration file is missing the path to the root directory - PDX_PathDocFiles.");
}
else
{
if (!Directory.Exists(pdxPathDocFiles))
{
Console.WriteLine("Directory not found");
}
else
{
try
{
Console.WriteLine("rootPath: " + pdxPathDocFiles);
PayDox_EPD19_T20_RGMEntities db = new PayDox_EPD19_T20_RGMEntities();
System.IO.DirectoryInfo rootDir = new DirectoryInfo(pdxPathDocFiles);
db.Database.ExecuteSqlCommand("TRUNCATE TABLE [FileDBRecord]");
db.SaveChanges();
db.Dispose();
Console.WriteLine("Remove data from table");
WalkDirectoryTree(rootDir, rootDir.ToString());
Console.WriteLine("All ok");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.WriteLine("Bye, Good Day.");
Console.WriteLine("Processing complete. Press any key to exit.");
Console.ReadKey();
}
static void WalkDirectoryTree(System.IO.DirectoryInfo root, string rootDir)
{
//Console.WriteLine("Go to folder: "+ root.FullName.Replace(rootDir, ""));
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
try
{
files = root.GetFiles("*.*");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
PayDox_EPD19_T20_RGMEntities db = new PayDox_EPD19_T20_RGMEntities();
foreach (var currentElement in files)
{
db.FileDBRecord.Add(new FileDBRecord { FileName = currentElement.FullName.Replace(rootDir, ""), FileSize = currentElement.Length });
}
db.SaveChanges();
db.Dispose();
subDirs = root.GetDirectories();
Parallel.ForEach(subDirs,
currentElement =>
{
try
{
WalkDirectoryTree(currentElement, rootDir);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
});
}
}
}
maybe we can fix your second code.. (untested, but may not throw the exception)
if you test it, let me know if it is faster..
static void Main(string[] args)
{
string pdxPathDocFiles = System.Configuration.ConfigurationManager.AppSettings["PDX_PathDocFiles"] as string;
if (string.IsNullOrEmpty(pdxPathDocFiles))
{
Console.WriteLine("In the configuration file is missing the path to the root directory - PDX_PathDocFiles.");
}
else
{
if (!Directory.Exists(pdxPathDocFiles))
{
Console.WriteLine("Directory not found");
}
else
{
try
{
Console.WriteLine("rootPath: " + pdxPathDocFiles);
PayDox_EPD19_T20_RGMEntities db = new PayDox_EPD19_T20_RGMEntities();
db.FileDBRecord.RemoveRange(db.FileDBRecord);
db.SaveChanges();
Console.WriteLine("Remove data from table");
IList<FileDBRecord> files = null;
System.IO.DirectoryInfo rootDir2 = new DirectoryInfo(pdxPathDocFiles);
try
{
files = rootDir2.GetFiles("*.*", SearchOption.AllDirectories).Select(x => new FileDBRecord { FileName = x.FullName.Replace(pdxPathDocFiles, ""), FileSize = x.Length });
Console.WriteLine("Reed {0} fileName", files.Length);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("You do not have permission to access one or more folders in this directory tree.");
Console.WriteLine(ex.Message);
return;
}
files.Foreach(db.FileDBRecord);
db.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("All ok");
}
}
Console.WriteLine("Bye, Good Day.");
}

How to upload directory to ftp using ftplib?

I have problem with upload all files to ftp: I use ftplib.
I have a function to upload:
static void DirSearch(string sDir, FtpConnection ftp)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
string dirname = new DirectoryInfo(d).Name;
if (!ftp.DirectoryExists(dirname))
{
ftp.CreateDirectory(dirname);
}
ftp.SetCurrentDirectory(dirname);
foreach (string f in Directory.GetFiles(d))
{
Uri uri = new Uri(f);
ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
}
DirSearch(d, ftp);
}
}
catch (System.Exception e)
{
MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
ok this function uload files but I have in local disc files:
UPLOAD
--DIR1
----DIR3
------FILE4
----FILE3
--DIR2
----DIR4
------FILE7
----FILE5
----FILE6
--FILE1
--FILE2
In serwer I have:
UPLOAD
--DIR1
----DIR3
------DIR2
--------DIR4
----------FILE7
--------FILE5
--------FILE6
------FILE4
----FILE3
I dont have files in first folder and dir tree is wrong
i think foult is in line ftp.SetCurrentDirectory(dirname);
Well, your function is the problem - when you enter the folder, and copy the files into it, you are not going back to the previous folder, instead you are going more deeply into tree.
Simple solution for this is to rewrite this function to go back from the directory once it has iterated through it:
static void DirSearch(string sDir, FtpConnection ftp)
{
try
{
// First, copy all files in the current directory
foreach (string f in Directory.GetFiles(d))
{
Uri uri = new Uri(f);
ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
}
// For all directories in the current directory, create directory if there is
// no such, and call this function recursively to copy files.
foreach (string d in Directory.GetDirectories(sDir))
{
string dirname = new DirectoryInfo(d).Name;
if (!ftp.DirectoryExists(dirname))
{
ftp.CreateDirectory(dirname);
}
ftp.SetCurrentDirectory(dirname);
DirSearch(d, ftp);
}
}
catch (System.Exception e)
{
MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally{
// Go back!
ftp.SetCurrentDirectory(".."); //untested, but it should be fine, as I don't see cdup command in ftplib
}
}
Yes, you're right. You can save and assign the current directory on each call. Try this:
static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
try
{
ftp.SetCurrentDirectory(currentDirectory);
foreach (string d in Directory.GetDirectories(sDir))
{
string dirname = new DirectoryInfo(d).Name;
if (!ftp.DirectoryExists(dirname))
{
ftp.CreateDirectory(dirname);
}
foreach (string f in Directory.GetFiles(d))
{
Uri uri = new Uri(f);
ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
}
string newCurrentDir = currentDirectory + dirname + "/";
DirSearch(d, ftp, newCurrentDir);
}
}
catch (System.Exception e)
{
MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
and method calling
DirSearch("your initial dir", your ftp connection, "/");
This code is good
static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
try
{
ftp.SetCurrentDirectory(currentDirectory);
foreach (string f in Directory.GetFiles(sDir))
{
Uri uri = new Uri(f);
ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
}
foreach (string d in Directory.GetDirectories(sDir))
{
ftp.SetCurrentDirectory(currentDirectory);
string dirname = new DirectoryInfo(d).Name;
if (!ftp.DirectoryExists(dirname))
{
ftp.CreateDirectory(dirname);
}
string newCurrentDir = currentDirectory + "/" + dirname ;
DirSearch(d, ftp, newCurrentDir);
}
}
catch (System.Exception e)
{
MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

Add strings to listbox in WPF using C# in a thread

I want to display the path of each file when a button is pressed.
What I have right now is a function that iterates through a folder and displays the paths, but only when the function is finished:
public void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
try
{
var fileEntries = Directory.GetFiles(targetDirectory);
foreach (var fileName in fileEntries)
{
ProcessFile(fileName);
}
}
catch (Exception e){}
// Recurse into subdirectories of this directory.
try
{
var subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
catch (Exception e){}
}
public void ProcessFile(string path)
{
myListBox.Items.Add(path);
}
This means that I have to wait before I can do something else.
How can I display the path of a file instantly when the function is running, so i don't have to wait before the function is finished, getting all the paths before displaying in the listbox?
I don't remember where I came across this piece of code, but if you modify your ProcessFile method to something like this it will update your UI after each item is added to your list.
public void ProcessFile(string path)
{
myListBox.Items.Add(path);
myListBox.ScrollIntoView(myListBox.Items[myListBox.Items.Count-1]);
Dispatcher.Invoke(new Action(delegate { }), DispatcherPriority.Background);
}
I think I remember reading somewhere that this "hack" is not recommended due to a number of other problems that might occur, but I can't remember what it was or where I read it. It does the job however.
Maybe someone else can enlighten us about what these problems are...
This makes your method run on another thread:
public void StartProcessThread(string targetDirectory)
{
Thread T = new Thread(new ParameterizedThreadStart(ProcessDirectory));
T.Start(targetDirectory);
}
public void ProcessDirectory(object objTargetDirectory)
{
string targetDirectory = (string)objTargetDirectory;
// Process the list of files found in the directory.
try
{
var fileEntries = Directory.GetFiles(targetDirectory);
foreach (var fileName in fileEntries)
{
ProcessFile(fileName);
}
}
catch (Exception e){}
// Recurse into subdirectories of this directory.
try
{
var subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
catch (Exception e){}
}
public void ProcessFile(string path)
{
Dispatcher.Invoke(new Action(() => {
myListBox.Items.Add(path);
}));
}

Categories