Easy Question, Delete all files in a path then recreate the path.
Hello,
Just an easy question for someone, not for me cause I don't know why it doesn't work, so if anyone could amend this please help:
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
else {
var hi = Directory.GetFiles(path);
for (int i = 0; i < hi.Length; i++) {
File.Delete(hi[i]);
}
Directory.Delete(path);
Directory.CreateDirectory(path);
}
What's the problem here? What doesn't work? The code looks "reasonable" on the surface, though I see no reason to remove and recreate the folder once you've removed all files in the folder...
I know, I just like to be clean and thorough, and recreate the directory and save file each time the program runs.
Answer by Kastenessen · Jun 23, 2016 at 01:24 AM
Sorry It does work LoL, Just had to go out then back into unity for it to update haha.
Brain fart!
Answer by jgodfrey · Jun 23, 2016 at 02:20 AM
Glad it's working, however you could replace all of the above with:
if (Directory.Exists(path)) { Directory.Delete(path, true); }
Directory.CreateDirectory(path);
That overload of Directory.Delete will remove the folder and all of its contents. So, no need to iterate through the contents...
Oh okay, that's a bit easier to look at.
I'm learning quick thanks.
I will use it thank you.
Your answer
Follow this Question
Related Questions
How can I setup a AI Bot to Pathfind around my small map? 0 Answers
Unity Hub crashes and can't create project 0 Answers
File Path Confusion for Sharing an Image 0 Answers
How to make an object follow a path when necessary but move forward when holding mouse button? 0 Answers
[Question] Get folder structure and filenames inside those folders at Runtime (Hololens), 0 Answers