C# - Access Denied Windows Files

Asked

Viewed 2,149 times

2

Hello I am making a software capable of scanning certain Windows files, where in it I leave filtered certain types of extensions, however, by placing to scan the following access message denied in certain folders.

The code would be as follows:

DirectoryInfo dir_Local = new DirectoryInfo(tb_UnidadeHD.Text);

string[] extensao = new string[] { "*.WsF", "*.lnk", "*.vbs", "*.vbe", "*.js", "*.com" };
string[] dir_Info = Directory.GetFiles(tb_UnidadeHD.Text, extensao.ToString(), SearchOption.AllDirectories); ;            

try
{
    foreach (string procura in dir_Info)
    {
        lb_Arquivos.Text = string.Format("Escaneando: {0}", procura);
        string result = ScanForVirus(procura);

        if (!dir_Local.Exists)
            MessageBox.Show(string.Format("Diretório de origem não existe ou não pôde ser encontrado: {0}", dir_Local), "AlegoTools", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else
        {
            if (result != string.Empty)
                listBox_Files.Items.Add(result);                            
            else
                MessageBox.Show(string.Format("Não foi encontrado nenhum vírus em {0}", dir_Local), "AlegoTools", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
catch { }

If you can give me a light, because I tried other means but without success, it simply can not scan certain parts of windows, even being as admin, shows as if it is without access by the program, and it is configured to run as administrator

  • Put a catch empty is the last thing you should do. You didn’t post the message. I need to leave, then I see if you can answer.

  • "of the following access message denied in certain folders." Did you ever notice the debug error? in my reply I am deducing that it is the exception Unauthorizedaccessexception

  • The question would not be the empty catch, because I already used the exception of Unauthorizedaccessexception and the error would be the same.

1 answer

2

Apparently and only a folder permission issue, you can try to check the permissions by accessing the properties on the security tab you can check the folder permissions.

Example

inserir a descrição da imagem aqui

Treating the exception

As the user does not have access to the way and notify him about his permission and if that is the case allow access to it the desired folder.

Code

catch (UnauthorizedAccessException ex)
{
  MessageBox.Show("No Momento Você Não Tem permissões para realizar esta operação , Contate o Administrador Do Sistema  ! Detalhes : " + ex.Message, "Alerta ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
  • It would not be possible to give permission, because the application is to scan on several machines, and it is difficult to have to give permission machine by machine, until there are several folders that give the access error denied, I have already given permission in some, but are several that give such problem. I wanted to see if you had any option to force him to read such folders.

  • 1

    In this case you should reformulate the question after its purpose and force the reading of the directories and not treat an error , but unfortunately in this case I do not know if it is possible .

  • This would force you to read the directories, but even the access level as general admin still does not get access even by placing to ignore the parts where you get the permission level error.

Browser other questions tagged

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