1
I am using the following line to create and record:
File.WriteAllText(caminho + cliente, xml);
In it I write an XML and then I deal with it as follows:
if (File.Exists(caminho + cliente))
{
XmlTextReader xmlLer = new XmlTextReader(caminho + cliente);
bool ultimaTag = false;
while (xmlLer.Read())
{
switch (xmlLer.NodeType)
{
case XmlNodeType.Element:
nomeElemento = xmlLer.Name.ToString();
break;
case XmlNodeType.Text:
switch (nomeElemento)
{
case "id_parcela":
objPedidoParcelas.IdParcela = int.Parse(xmlLer.Value);
break;
case "id_pedido":
objPedidoParcelas.IdPedido = int.Parse(xmlLer.Value);
break;
case "forma_pagamento":
objPedidoParcelas.FormaPagamento = Utils.RemoverAcentos(xmlLer.Value.ToString().ToUpper());
break;
case "data_vencimento":
objPedidoParcelas.DataVencimento = xmlLer.Value.ToString();
break;
case "valor":
objPedidoParcelas.Valor = xmlLer.Value.ToString();
break;
case "data_pagamento":
objPedidoParcelas.DataPagamento = xmlLer.Value.ToString();
break;
case "data_confirmacao":
objPedidoParcelas.DataConfirmacao = xmlLer.Value.ToString();
break;
case "valor_pago":
objPedidoParcelas.ValorPago = xmlLer.Value.ToString();
break;
case "local_pagamento":
objPedidoParcelas.LocalPagamento = Utils.RemoverAcentos(xmlLer.Value.ToString().ToUpper());
break;
case "observacao":
objPedidoParcelas.Observacao = Utils.RemoverAcentos(xmlLer.Value.ToString().ToUpper());
break;
case "id_forma_pagamento":
objPedidoParcelas.IdFormaPagamento = int.Parse(xmlLer.Value.ToString());
break;
case "qt_parcelas":
objPedidoParcelas.QtdParcelas = int.Parse(xmlLer.Value.ToString());
break;
case "status":
objPedidoParcelas.Status = Utils.RemoverAcentos(xmlLer.Value.ToString().ToUpper());
break;
}
break;
}
}
}
Soon I try to delete the file:
DirectoryInfo di = new DirectoryInfo(caminho);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
Says the file is being used by another process.
The file does not exist, it is created in order to write the XML and then read it and then delete.
Normally you are not allowed to do this, the file is open or something like that. It may not close the file you just saved.
Thread.Sleep(1000)
This sounds like a scam. The fact is that we do not know what happens, at least not only with this information.– Maniero
yes it is a gambiarra.. to see if closes the file
– novato
Files do not close themselves. No use making macumba.
– Maniero
Truth... have any suggestions?
– novato
Which folder are you creating the file?
– Jéf Bueno
is a folder inside the Bin folder of the Visual Studio project.. is called XML
– novato
You try to delete right after creating?
– Jéf Bueno
No... some things after.. but that has nothing to do with the file.. I create... write an xml, use the file for something else and try to delete
– novato
Need to go closing the problem, find out which file is causing it, check if it is open on the operating system, who has it open, is your application? see why it is open. Go running and see where the problem is. This code is correct, there is problem elsewhere.
– Maniero
Do you use the file in the application? Probably the problem is in this "use the file for something else and try to delete".
– Jéf Bueno
Place whole the code of
if (File.Exists(...))
– Jéf Bueno