- Home /
Sharing violation when deleting a directory used by a WWW object
If I load a local file using unity's www class, then later try to delete the folder containing that file, I get an exception. Here's the full explanation.
I have some code which attempts to sync some files from the internet to the local disk. The files are then used by the app and, images in particular, are loaded loaded using the www class by declaring it like this; new WWW(@"file://" + imageFilePath). However, later the user might want to resync those files, in which case I was deleting the directory which contained them by calling Directory.Delete(outDirPath, true), to delete the directory and all the files it contains so I could then create the directory and start from scratch.
This all works fine on windows but on android attempting to delete the directory throws an exception saying that there was a sharing violation on the path. I tracked this down to the www objects - if I never create any accessing that directory, deleting it doesn't throw the exception. So to try and fix this I first made sure that I didn't have any www objects hanging around in memory which could potentially be keeping a lock on the files they loaded but the exception still got thrown.
Currently I'm working around this by not deleting the directory, just the files it contains, by calling;
foreach(var file in Directory.GetFiles(outDirPath))
{
File.Delete(file);
}
Which works fine even when I have created www objects but worries me as it suggests there really is something I'm not clearing up which still has a lock on the directory. Has anyone else encountered this?
This is not a good idea on Android as a lot of people have limited Data downloads. They don't want to be repeatedly downloading more data that is potentially already on the device - Just some advice.
Fair point, however I show warnings about downloads which are going to be of any reasonable size. Also ideally this would be a tablet app I'm developing - where most of the time a connection to the internet means wifi. Personally though I find it annoying when apps warn me about data use, though I readily admit I maybe unusual in that respect. Still the average smart phone is full of apps that don't warn the user; Facebook, Instagram etc.
However, this specific issue doesn't look like it's related to actually downloading as it only occurs when I use www object to load a local file and then after try to delete the directory containing it. I imagine it's just coincidence that the local file in this case was downloaded from the internet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get file list from a directory on Android 4 Answers
Share image on selected media 1 Answer
Android Internet not connecting 1 Answer
Any way around WWW timeout on Android? 2 Answers
Android Exception getInputStream() is not available 0 Answers