DirectoryInfo[] directories =
di.GetDirectories("*.*", SearchOption.AllDirectories);
问题是我无法访问某些文件夹,因此出现异常。
例如我无法访问的程序文件。
有什么解决方法吗?
编辑: 这是代码。如您所见,我已经配置了代码,因此如果发生异常它会继续。当它发生时,我只收到拒绝访问路径“C:\Arquivos de Programas”的消息。并且没有文件或文件夹
如何避免这种行为并在屏幕上打印我有权限的行为?
谢谢
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;
}