Reputation: 919
I want to delete a Directory and all its files and it has files/Directories with a very long path.
The file I'm trying to delete has a long path (longer than 260 characters).
How can I delete this file in spite of its length? I'm using the following code:
foreach (string archiveFolder in Archives)
{
try
{
DateTime creationTime = Directory.GetCreationTime(archiveFolder);
DateTime now = DateTime.Now;
DateTime passDate = creationTime.AddDays(numDaysBack);
if (passDate.CompareTo(now) < 0)
{
try
{
Directory.Delete(archiveFolder, true);
}
catch (Exception e)
{
}
//System.Console.WriteLine(creationTime);
}
}
catch (Exception e)
{
}
}
Upvotes: 2
Views: 3209
Reputation: 1313
I fixed the problem of having a file with long path down inside the directory structure of the directory I'm deleting by prepending \\?\
(long path specifier) to the Directory.Delete argument.
So in your cause it would be
Directory.Delete(@"\\?\" + archiveFolder, true);
Upvotes: 3
Reputation: 7773
Directory.Delete is meant to delete directories, to delete files use File.Delete
They both reside in System.IO
, the change should be trivial.
Upvotes: 1