Sono riuscito a fare quello ce volevo...
solo che vorrei una prograssione dci file contati ogni tot secondi...
questo il codice implenmentato...
Albero
using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
namespace ImpronteCS
{
class FileSystemTreeView : TreeView
{
/// <summary>Lista dei files caricati</summary>
public List<string> FileList;
/// <summary>Aggiungere le sottodirectory?</summary>
public bool SubFolders = false;
/// <summary>Il formato dei files da caricare</summary>
public string Format = string.Empty;
/// <summary>Conteggio dei file [si può omettere]</summary>
public int FileCount
{
get { return (FileList != null) ? FileList.Count : -1; }
}
/// <summary>Costruttore</summary>
public FileSystemTreeView()
{
this.BeforeExpand += new TreeViewCancelEventHandler(FS_BeforeExpand);
this.AfterCollapse += new TreeViewEventHandler(FS_AfterCollapse);
loader.WorkerReportsProgress = true;
loader.WorkerSupportsCancellation = true;
loader.DoWork += new DoWorkEventHandler(this.Work);
loader.ProgressChanged += new ProgressChangedEventHandler(this.Progress);
loader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.Completed);
}
private void FS_AfterCollapse(object sender, TreeViewEventArgs e)
{
//Pulisci i nodi.
for (int i = 0; i < e.Node.Nodes.Count; ++i)
{
foreach (string file in FileList)
{
if (file.EndsWith(e.Node.Nodes[i].Text))
{
FileList.Remove(file);
break;
}
}
}
e.Node.Nodes.Clear();
e.Node.Nodes.Add(".");
Count(this, new SystemTreeArgs(FileList.Count));
}
private void FS_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
try
{
e.Node.Nodes.Clear();
string needle = e.Node.Tag as string;
string[] folders = Directory.GetDirectories(needle);
string[] files = Directory.GetFiles(needle);
//Carico le sottodirectory?
if (SubFolders)
{
foreach (string folder in folders)
{
try
{
string[] s_folders = Directory.GetDirectories(folder);
string[] s_files = Directory.GetFiles(folder);
TreeNode t_folder = getNode(folder);
foreach (string s_folder in s_folders)
t_folder.Nodes.Add(getNode(s_folder));
foreach (string s_file in s_files)
{
//E' il formato che voglio?
if (s_file.EndsWith(Format))
{
//Allora aggiungilo.
//if(!FileList.Contains(s_file))
//FileList.Add(s_file);
t_folder.Nodes.Add(getNode(s_file));
}
}
e.Node.Nodes.Add(t_folder);
}
catch (IOException ioe)
{
//Fai niente
}
catch (UnauthorizedAccessException uae)
{
//Fai niente
}
finally
{
//Fai niente
}
}
}
foreach (string file in files)
{
//E' il formato che voglio?
if (file.EndsWith(Format))
{
//Allora aggiungilo.
if (!FileList.Contains(file))
FileList.Add(file);
e.Node.Nodes.Add(getNode(file));
}
}
}
catch (IOException ioe)
{
//Fai niente
}
catch (UnauthorizedAccessException uae)
{
//Fai niente
}
finally
{
//Fai niente
}
Count(this, new SystemTreeArgs(FileList.Count));
}
/// <summary>Carica la struttura</summary>
/// <param name="needle">Path da cui partire</param>
public void Load(string needle)
{
//loader.RunWorkerAsync(new object[] { needle, Format, SubFolders});
if (FileList == null)
FileList = new List<string>();
//Cancella la lista dei files.
FileList.Clear();
//Cancella i nodi
this.Nodes.Clear();
//prendi il nodo principale.
TreeNode n_node = getNode(needle);
n_node.Nodes.Add(".");
this.Nodes.Add(n_node);
//Espando il nodo root.
this.Nodes[0].Expand();
Count(this, new SystemTreeArgs(FileList.Count));
if (!loader.IsBusy)
{
loader.RunWorkerAsync(new object[] { needle, Format, SubFolders });
}
else
{
loader.CancelAsync();
}
}
/// <summary>Crea il nodo dell'albero</summary>
/// <param name="path">La cartella del Nodo.</param>
/// <returns>TreeNode</returns>
private TreeNode getNode(string path)
{
TreeNode r_node = new TreeNode();
int limit = path.LastIndexOf('\\') + 1;
string name = path.Substring(limit, (path.Length - limit));
if (name.Length < 1)
name = path.Substring(0, path.Length - 1);
r_node.Text = name;
r_node.Tag = path;
return r_node;
}
BackgroundWorker loader = new BackgroundWorker();
private void Work(object sender, DoWorkEventArgs e)
{
//Conteggia i files [si può omettere]
object[] data = e.Argument as object[];
int Cnt = 0;
FileSystemCounter.Count(data[0] as string, data[1] as string, (bool)data[2], ref Cnt);
e.Result = Cnt;
}
private void Progress(object sender, ProgressChangedEventArgs e)
{
}
private void Completed(object sender, RunWorkerCompletedEventArgs e)
{
if ((!e.Cancelled) && (e.Error == null))
TotalCount(this.TotalCount, new SystemTreeArgs((Convert.ToInt32(e.Result))));
else
TotalCount(this.TotalCount, new SystemTreeArgs(0));
}
public delegate void CountChanged(object sender, SystemTreeArgs e);
public event CountChanged Count;
public delegate void TotalCountChanged(object sender, SystemTreeArgs e);
public event TotalCountChanged TotalCount;
}
public class SystemTreeArgs : EventArgs
{
private int count;
public SystemTreeArgs(int count) { this.count = count; }
public int Count { get { return count; } }
}
}
using System;
using System.IO;
namespace ImpronteCS
{
public class FileSystemCounter
{
public static void Count(string path, string file_type, bool sub_folders, ref int count)
{
foreach (string file in Directory.GetFiles(path))
{
try
{
if(file.EndsWith(file_type))
count++;
}
catch (IOException ioe) { }
catch (UnauthorizedAccessException uae) { }
finally { }
}
if (sub_folders)
foreach (string folder in Directory.GetDirectories(path))
{
try
{
Count(folder, file_type, sub_folders, ref count);
}
catch (IOException ioe) { }
catch (UnauthorizedAccessException uae) { }
finally { }
}
}
}
}
ora, vorrei che ogni tot secondi, la mia bellissima classe di conteggio file [che con 50000+ file ci mettono ben 1 minuto], mandassero al bgw un bel messaggino "Ho processato X files"...
come faccio?
Non c'è emozione; c'è pace.
Non c'è ignoranza; c'è conoscenza.
Non c'è inquietudine; c'è serenità.
Non c'è caos; c'è armonia.
Non c'è morte; c'è la Forza.