Ho creato un'applicazione che guarderà una determinata cartella solo per i file appena creati e la elenca nella lista, ora voglio fare ogni volta che rileverà il file l'applicazione lo leggerà e visualizzerà il testo nella casella di riepilogo, ho quasi avuto perché a volte quando rileva 2 o 3,4,5,6 file ecc a volte ok ma a volte anche verrà richiesto l'errore "Il processo non può accedere al file" C: \ Users \ PHWS13 \ Desktop \ 7.request.xml 'perché è utilizzato da un altro processo. ".Il file viene utilizzato da un altro processo con FileSystemWatcher e Task
Come risolvere il problema? Ecco il mio codice:
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (!listBox1.Items.Contains(e.FullPath))
{
//add path
listBox1.Items.Add(e.FullPath + "" + DateTime.Now.ToString());
//get the path
path = e.FullPath;
//start task
startTask();
}
}
private void startTask()
{
//start task
Task t = Task.Factory.StartNew(runThis);
}
private void runThis()
{
//get the path
string get_the_path = path;
XDocument doc = XDocument.Load(get_the_path);
var transac = from r in doc.Descendants("Transaction")
select new {
InvoiceNumber = r.Element("InvoiceNumber").Value,
};
listBox2.Invoke((MethodInvoker)delegate() {
foreach(var r in transac){
listBox2.Items.Add(r.ToString());
}
});
ancora lo stesso, provato! :( – GrayFullBuster
hey è una correzione, ho appena aggiunto questo "FileShare.ReadWrite" – GrayFullBuster
Non funziona Ho fatto lo stesso –