在执行Directory.Delete之前检查文件是否已被访问的一种方法? [英] A way to check whether the files are being accessed before I do Directory.Delete?

查看:75
本文介绍了在执行Directory.Delete之前检查文件是否已被访问的一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我每天都会将一些文件复制到目标目录.
在移动新文件之前,我想删除目标文件夹下的旧文件,可能使用
System.IO.Directory.Delete()

但是我的问题是,目标文件夹位于公司中每个人都可以访问的公用驱动器上,并且我不想在其他人可能正在使用这些文件的情况下清空该文件夹.

我在想是否有一个Class可以在进入并删除所有内容之前检查特定目录中文件的状态.(我知道如果不考虑文件的状态并尝试删除它会得到IOException他们)谢谢

Hi guys,

I''m copying over some files to a target directory on a daily basis.
Before I move the new files over, I''d like to delete the old files under the target folder, possibly using
System.IO.Directory.Delete()

But my problem is, the target folder is on a public drive where everybody in the company could access, and I don''t want to empty the folder while someone else might be using those files.

I''m thinking whether there''s a Class that check the status on the files in a specific directory before I come in and delete everything.(I know I would get an IOException if I disregard the status of the files and try to delete them) Thank you

推荐答案

没有此类.您可以尝试打开这些文件,拒绝所有共享访问,但是检查存在问题.

在您检查支票到开始删除文件之间,其他用户可以打开它们,使支票无用.

尝试删除文件的最佳选择,如果删除失败,则进行适当的错误处理.
There is no such class. You could try to open those file, denying all shared access to them, but there''s a problem with checking.

Between the time you check and the time you start to delete the files, another user can open them, rendering your check useless.

The best option to to just try and kill the files, with appropriate error handling if a delete fails.


这篇关于在执行Directory.Delete之前检查文件是否已被访问的一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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