【发布时间】:2012-07-29 14:07:13
【问题描述】:
可能重复:
.NET - Check if directory is accessible without exception handling
我正在使用 NET 3.5 和 C# 在 Visual Studio 2010 中创建一个小文件浏览器,并且我有这个功能来检查目录是否可访问:
RealPath=@"c:\System Volume Information";
public bool IsAccessible()
{
//get directory info
DirectoryInfo realpath = new DirectoryInfo(RealPath);
try
{
//if GetDirectories works then is accessible
realpath.GetDirectories();
return true;
}
catch (Exception)
{
//if exception is not accesible
return false;
}
}
但我认为对于大目录,尝试获取所有子目录以检查目录是否可访问可能会很慢。 我使用此功能来防止在尝试浏览受保护的文件夹或没有光盘的 cd/dvd 驱动器时出错(“设备未就绪”错误)。
是否有更好的方法(更快)来检查应用程序是否可以访问目录(最好在 NET 3.5 中)?
【问题讨论】:
-
这是否适用于“可访问” Directory.Exists ( Path.Combine( RealPath + "\." ) )
-
你违背了 Windows 资源管理器的工作方式。为什么不显示不可访问的文件夹?如果用户看不到它,他就不会知道他有一个 CD 驱动器。
-
用户不知道该目录是否可访问,如果用户双击“不可访问”目录c#返回错误
标签: c# visual-studio-2010 directoryinfo getdirectories