C#如何忽略异常继续执行代码呢?
string str = "c:\\";
int i = 0;
foreach (string s in Directory.GetFiles(str, "*", SearchOption.AllDirectories))
{
i += 1;
}
像以上代码。一运行就会报错,说是某某目录拒绝访问。所以我想问问有没有什么方法可以忽略这个异常让程序继续运行,跳到下一个目录呢?
------解决方案--------------------public int ddddd(string path)
{
int i = 0;
string[] strFile = Directory.GetFiles(path);
i = strFile.Length;
string[] strDirectory = Directory.GetDirectories(path);
foreach (string item in strDirectory)
{
try
{
i += ddddd(item);
}
catch (Exception)
{
continue;
}
}
return i;
}