Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Nelek 5-Apr-12 19:13pm    
And the question is...?
Shahin Khorshidnia 5-Apr-12 20:34pm    
Not clear,
Do you want to delete a file if it's not being used by another process?
Or you want to delete files that are at least once used?
Or you want to prevent the runtime exception?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900