DirectoryInfo[] directories =
di.GetDirectories("*.*", SearchOption.AllDirectories);
The problem is that some folders I am not able to access thus I get an exception.
Program files for example I cannot access.
Is there any workaround for that?
Edit: Here is the code. As you can see I have configure the code so it continue if happens an exception. When it occurs I only get the message Access to the path 'C:\Arquivos de Programas' is denied. and no files or folder
How can I avoid this behavior and print on screen the ones I have permission?
Thanks
try
{
//Resgata todos os drivers Lógicos do Sistema
DriveInfo[] allDrives = DriveInfo.GetDrives();
//Cria uma lista não ordenada para os DRIVERS
Response.Write("<ul class=\"jqueryFileTree\" style=\"display: none;\">\n");
//Itera sobre cada driver no array
foreach (DriveInfo drive in allDrives)
{
if (drive.IsReady == true)
{
try //GetDirectories
{
//Para cada driver cria um LI A
Response.Write("\t<li class=\"drive collapsed\"><a href=\"#\" rel=\"" + drive.ToString() + "\">" + drive.ToString() + "</a>\n");
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(drive.ToString());
DirectoryInfo[] directories = di.GetDirectories("*.*", SearchOption.AllDirectories);
Response.Write("<ul>");
//Itera sobre os subdiretórios de cada driver
foreach (System.IO.DirectoryInfo di_child in directories)
{
Response.Write("\t<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + drive + di_child.Name + "/\">" + di_child.Name + "</a>\n");
Response.Write("<ul>");
//Itera sobre todos os arquivos do diretório
foreach (System.IO.FileInfo fi in di.GetFiles())
{
string ext = "";
if (fi.Extension.Length > 1)
{
ext = fi.Extension.Substring(1).ToLower();
}
Response.Write("\t<li class=\"file ext_" + ext + "\"><a href=\"#\" rel=\"" + drive + fi.Name + "\">" + fi.Name + "</a></li>\n");
}// Arquivos
Response.Write("</ul></li>");
}// subdiretorio
Response.Write("</ul></li>");
}
catch (UnauthorizedAccessException e)
{
Response.Write(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Response.Write(e.Message);
continue;
}
catch (Exception e)
{
Response.Write(e.Message);
continue;
}
}//isReady
}///drive
Response.Write("</ul>");
}
catch (Exception)
{
throw;
}