Reputation: 11
I am using Zetalongpath to access pdf files for long path.
Files will be uploaded from the location like 'D:\Active Folder\ReadyTobeTransfer\ABC\XYZ' to 'D:\Upload Intake\ABC\XYZ'
i want to delete folder ABC recursively after successful upload, as this is the requirement of client.
here is my code to save the uploaded files from browser.
var FilingDocs = new List<HttpPostedFileBase>();
for (var i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].FileName.Substring(Request.Files[i].FileName.LastIndexOf('.') + 1).ToLower() == "pdf")
{
FilingDocs.Add(Request.Files[i]);
}
}
foreach (HttpPostedFileBase file in FilingDocs)
{
if (file != null)
{
// Read bytes from http input stream
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.ContentLength);
string fileName;
if (file.FileName.LastIndexOf('\\') > 0)
{
fileName = file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1);
}
else
{
fileName = file.FileName;
}
ZlpIOHelper.WriteAllBytes(fullPath + '\\' + fileName, binData);
file.InputStream.Close();
b.Close();
}
}
//DB insert method
//Code to delete Uploaded folder
string ParentFolder = "D:\Active Folder\ReadyTobeTransfer\ABC";
string ChildFolder = "D:\Active Folder\ReadyTobeTransfer\ABC\XYZ";
//Delete Files from ChildFolder
string[] files = Directory.GetFiles(ChildFolder, "*.pdf");
try
{
foreach (string file in files)
{
//Remove read only access
System.IO.File.SetAttributes(file, FileAttributes.Normal);
System.IO.File.Delete(file);
}
//Sleep for 5 second
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 5));
//Check whether Child Folder Exist
if (ZlpIOHelper.DirectoryExists(ChildFolder))
{
//Delete Uploaded Files Folder
Directory.Delete(ChildFolder, true);
}
//Sleep for 5 second
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 5));
//Check whether Parent Folder Exist
if (Directory.Exists(ParentFolder))
{
//Get Parent File information to check whether it contains any file or folder
ZlpDirectoryInfo DeleteParentFolder = new ZlpDirectoryInfo(ParentFolder);
if (DeleteParentFolder.GetFiles().Length == 0 && DeleteParentFolder.GetDirectories().Length == 0)
{
//Delete Parent Folder
Directory.Delete(ParentFolder, true);
}
}
}
catch (IOException)
{
if (ZlpIOHelper.DirectoryExists(ChildFolder))
Directory.Delete(ChildFolder);
if (ZlpIOHelper.DirectoryExists(ParentFolder))
Directory.Delete(ParentFolder);
}
catch (UnauthorizedAccessException)
{
if (ZlpIOHelper.DirectoryExists(ChildFolder))
Directory.Delete(ChildFolder);
if (ZlpIOHelper.DirectoryExists(ParentFolder))
Directory.Delete(ParentFolder);
}
the delete code gives error 'Directory is not empty.' but the files gets deleted from the location 'D:\Active Folder\ReadyTobeTransfer\ABC\XYZ' however it doesn't delete the parent and child folder.
this is the error message
The directory is not empty. StackTrace:- at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound) at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
Upvotes: 0
Views: 390