Deleted file deletion in C #

2

I'm using the following line to create and write:

File.WriteAllText(caminho + cliente, xml);

In it I record an XML and then I treat 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();
}

It says the file is being used by another process.

The file does not exist, it is created with the purpose of writing the XML and then reading it and then deleting it.

    
asked by anonymous 06.10.2016 / 22:05

2 answers

7

The problem is that the file is being used by XmlTextReader , in that line

XmlTextReader xmlLer = new XmlTextReader(caminho + cliente);

You need to release it using Dispose() . . Add this line at the end of the code:

xmlLer.Dispose();

You can also use using to avoid forgetting Dispose() . With using it is possible to have sure that the stream (the "file") will close, even if there is some exception in the block, because "under it's nothing more than a try { } finally { recurso.Dispose(); } - you can see more details here

using(XmlTextReader xmlLer = new XmlTextReader(caminho + cliente))
{
   // ao final do bloco o arquivo será "liberado"   
}
    
06.10.2016 / 22:27
6

From what was posted the file is probably open. The line that opens:

XmlTextReader xmlLer = new XmlTextReader(caminho + cliente);

Do not close the file.

The correct thing is to open with using , something like this:

using (xmlLer = new XmlTextReader(caminho + cliente)) {
    //faz tudo o que precisa
}

This is the only way to ensure closure occurs. Whenever the class implements the interface IDisposable it should do so.

You have examples in the documentation for XmlReader (of which XmlTextReader inherits).

You have a ask more details about the subject .

    
06.10.2016 / 22:29