1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
//Méthode pour lancer le scan.
public void scan(String sourceDir, int recursionLvl) {
ParameterizedThreadStart operation = new ParameterizedThreadStart(scanDir);
// Instanciation des threads.
Thread t1 = new Thread(operation);
t1.Start(new Parameter(sourceDir, recursionLvl));
}
const int HowDeepToScan = 7;
public void scanDir(object o)
{
Parameter p = o as Parameter;
if (p.RecursionLvl <= HowDeepToScan) {
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(p.SourceDir);
foreach (string fileName in fileEntries) {
try {
AppendText("\n" + fileName);
} catch (Exception e) {
AppendText("\n" + e.Message);
}
}
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(p.SourceDir);
foreach (string subdir in subdirEntries)
// Do not iterate through reparse points
try {
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)
scanDir(new Parameter(subdir, p.RecursionLvl + 1));
} catch (Exception e) {
AppendText("\n" + e.Message);
}
}
}
// callback qui t'empeche des problèmes avec l'interface
private void AppendText(String s)
{
if (richTextBox1.InvokeRequired) {
AppendTextCallback d = new AppendTextCallback(AppendText);
this.Invoke(d, new object[] { s });
} else {
richTextBox1.AppendText(s);
}
}
delegate void AppendTextCallback(String s);
// objet qui te permet d'envoyer à ta méthode scanDir les infos nécéssaires
class Parameter
{
private String sourceDir;
private int recursionLvl;
public Parameter(String sourceDir, int recursionLvl)
{
this.sourceDir = sourceDir;
this.recursionLvl = recursionLvl;
}
public String SourceDir
{
get { return this.sourceDir; }
}
public int RecursionLvl
{
get { return this.recursionLvl; }
}
} |