Denied file deletion on C#

Asked

Viewed 113 times

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.

  • yes it is a gambiarra.. to see if closes the file

  • 2

    Files do not close themselves. No use making macumba.

  • Truth... have any suggestions?

  • Which folder are you creating the file?

  • is a folder inside the Bin folder of the Visual Studio project.. is called XML

  • You try to delete right after creating?

  • 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

  • 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.

  • Do you use the file in the application? Probably the problem is in this "use the file for something else and try to delete".

  • 1

    Place whole the code of if (File.Exists(...))

Show 6 more comments

2 answers

8


The problem is that the file is being used by XmlTextReader, along those lines

XmlTextReader xmlLer = new XmlTextReader(caminho + cliente);

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

xmlLer.Dispose();

You can also use the block using to avoid forgetting the Dispose(). With the using it is possible to have certainty that the stream (the "file") will be closed, even if some exception occurs in the block, because, for "under the table" it is nothing more than a try { } finally { recurso.Dispose(); } - further details can be found here

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

6

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

XmlTextReader xmlLer = new XmlTextReader(caminho + cliente);

Does not close the file.

The correct is to open with using, something like that:

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

I put in the Github for future reference.

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

Has examples in the documentation of XmlReader (of which the XmlTextReader inherits).

Has a question with more details on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.