我如何在c#中跳过错误 [英] How do I Skip the error in c#

查看:384
本文介绍了我如何在c#中跳过错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成特定路径中的文件列表,但每当我要生成文件列表时,我都会收到错误消息访问路径xyz被拒绝。

但是我想要跳过此错误并继续生成文件列表。



我如何归档此任务。

解决方案

基本上,你不能。

如果文件夹被拒绝访问,AllDirectories搜索将终止并且无法重新启动它。

您唯一的选择是手动获取目录中的文件,然后使用try-catch块递归扫描其所有子目录以查找访问问题并忽略它们。


  foreach  var 文件 个文件)
{
尝试
{
// 您的代码
{
catch
{
}
}


I want to generate list of files in specific path but whenever i am going to generate list of files I am getting error that "Access to the path xyz is denied."
but I want to skip this error and continue generating list of files.

So how I can archive this task.

解决方案

Basically, you can't.
If access is denied to a folder, the "AllDirectories" search will terminate and there is no way to "restart" it.
You only option is to fetch the files in a directory manually, then recursively scan all it's subdirectories using a try-catch block to find access problems and ignore those.


foreach (var file in files)
{
    try
    {
        // your code here
    {
    catch
    {
    }
}


这篇关于我如何在c#中跳过错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆