我想在删除C#时跳过正在使用的文件 [英] I want to skip file in use while delete C#

查看:374
本文介绍了我想在删除C#时跳过正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个软件来清理临时文件夹中的文件但是在删除进度中,一些正在使用的文件会出错。我想跳过它并继续删除其他文件。



我尝试过:



我想在c#

I try to create a software to clean files in temp folder but in delete progress, some files in use make error. I want to skip it and continue delete other file.

What I have tried:

I want to skip file in use while delete in c#

推荐答案

中删除正在使用的文件。检查的唯一方法是尝试删除它并查看它是否失败。

如果是,你会得到一个 IOException ,所以你可以使用 try ... catch 阻止检测并忽略它:

The only way to check is to try and delete it and see if it fails.
If it does, you will get an IOException, so you could use a try...catch block to detect and ignore it:
try
   {
   ... delete the file here ...
   }
catch (IOException ex)
   {
   ... log or ignore the error here ...
   }


这篇关于我想在删除C#时跳过正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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