-2

trivial error, time performs the normal process of deleting files and another hour to perform the same process I get this error.

This is an update of a website folder that is being accessed through IIS, then this process occurs while people are logged on the website.

You have no idea what can be?

Exception.Message =

Access to the path 'W:\inetpub\wwwroot\site\Recursos\Css\Fonts\roboto-light_0-webfont.ttf' is denied.

Message: Access to the path 'W:\inetpub\wwwroot\site\Recursos\Css\Fonts\roboto-light_0-webfont.ttf' is denied. StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.SetAttributes(String path, FileAttributes fileAttributes) at ServicoAtualizador.Utilitarios.Pacote.DeletarArquivos(List1 Ignorar, String[] files) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Utilitarios\Pacote.cs:line 77 at ServicoAtualizador.Utilitarios.Pacote.DeleteDirectory(String target_dir, List1 Ignorar, Boolean deletarRaiz) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Utilitarios\Pacote.cs:line 46 at ServicoAtualizador.Utilitarios.Pacote.DeleteDirectory(String target_dir, List1 Ignorar, Boolean deletarRaiz) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Utilitarios\Pacote.cs:line 37 at ServicoAtualizador.Utilitarios.Pacote.DeleteDirectory(String target_dir, List1 Ignorar, Boolean deletarRaiz) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Utilitarios\Pacote.cs:line 37 at ServicoAtualizador.Utilitarios.Pacote.DeleteDirectory(String target_dir, List1 Ignorar, Boolean deletarRaiz) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Utilitarios\Pacote.cs:line 37 at ServicoAtualizador.Processamento.ProcessarAtualizacao(String Aplicacao, DirectoryItem arquivo, AtualizacaoItemProduto itemProduto, AtualizacaoItem item, List1 colecao) in d:\WOOBA_WORKSPACE_2010\WoobaWebDesk\Source\Development\aspWoobaWebDesk\ServicoAtualizador\Processamento.cs:line 227 Source: mscorlib

CODE:

public static void DeleteDirectory(string target_dir, List<string> Ignorar = null, bool deletarRaiz = true)
    {
        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);

        DeletarArquivos(Ignorar, files);

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        if (deletarRaiz)
        {
            int contador = 0;
            while (Directory.GetFiles(target_dir).Any()) // prevent directory is not empty
            {
                System.Threading.Thread.Sleep(200);
                DeletarArquivos(Ignorar, Directory.GetFiles(target_dir));
                contador++;
                if (contador >= 3)
                    throw new ArgumentException("Não possível deletar os arquivos, diretório:" + target_dir);
            }

            Directory.Delete(target_dir, false);
        }
    }

    private static void DeletarArquivos(List<string> Ignorar, string[] files)
    {
        foreach (string file in files)
        {
            FileInfo Info = new FileInfo(file);

            if (Ignorar != null)
            {
                if (Ignorar.Any(o => o.Equals(Info.Name, StringComparison.OrdinalIgnoreCase)))
                    continue;
            }

            if (Info.IsReadOnly)
                Info.IsReadOnly = false;

            var attr = File.GetAttributes(file);

            if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                File.SetAttributes(file, attr ^ FileAttributes.ReadOnly);
            }
            File.SetAttributes(file, FileAttributes.Normal); //...error here
            File.Delete(file);
        }
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

There are any number of reasons why you'd get that exception, but without knowing more about what's going on, it's impossible to say. Could be that if the file was uploaded via IIS, then IIS might still have a handle on it. Are you sure you disposed of any streams or file access?

Either way, I would wrap the attempt to delete the file in a try/catch block, and log any failures. That way, your application won't crash, and you can easily see what files are causing problems. Could be that next time it tries to delete, it will work. Depends on how long the file is being held.

As I said, without more background, we can't say, but the try/catch is something you should always do in a case like this, so I would add that and see how you get on.

Hope that helps.

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106