0
sdel = Server.MapPath("~/Media_Extracted_Content" + "/" + sfolder);
Directory.Delete(sdel,true);

'sfolder' contains different sub folder and all sub folder have contains different items. All items like image file, audio file, video file opened in browser .I am copying that items from this existing location to new location and after that I have to delete this directory from my system. Whenever I am trying to to this it shows error that Directory is not empty. Also, when I am trying to delete individual items from sub folder it is showing error that this file is being used by another process. Please help me.

Abhay Singh
  • 1,595
  • 1
  • 23
  • 40
  • Duplicate of http://stackoverflow.com/questions/1040/how-do-i-delete-a-file-which-is-locked-by-another-process-in-c – 3dd Jun 07 '14 at 05:53
  • But there are not any solution for this problem. Please explain and suggest me better answer. Thanks in advance.... – Abhay Singh Jun 07 '14 at 05:55
  • @abhay9455 - really - there are plenty of approaches... Did you at least already figured out what "other process" locked the file? – Alexei Levenkov Jun 07 '14 at 05:58
  • Did you move these files into the folder using your website? – 3dd Jun 07 '14 at 06:01
  • no.... I am trying to copy these files into another location after that I am deleting. But same issues as mentioned in my error explanation – Abhay Singh Jun 07 '14 at 06:02
  • Press delete and leave the messagebox with retry open. Hit recycle on your application pool and keep pressing retry until it works or until you're satisfied it doesn't work :) – Silvermind Jun 07 '14 at 07:53

2 Answers2

0

I think at Server or at Hosting you have not given the permision to the folder which are, allow READ and WRITE to the Folder.

Naisarg Parmar
  • 759
  • 8
  • 25
0

Please Try This Two FUNCTION/Method.
You only have to do is paste both function in class file(eg class1.cs).
In (aspx.cs)Assign Value to source and destination
For Example source = Server.MapPath("~/Media_Extracted_Content/" + sourcefolder);
destination = Server.MapPath("~/Media_Extracted_Content/" + destinationfolder);
And Call classobject.MoveFiles(source, destination,true);

public void createfolder(string directorypath)
{
    // CREATE folder
    try
    {
        Directory.CreateDirectory(directorypath);
    }
    catch (Exception ex)
    { }
}
public void MoveFiles(string source, string destination, bool overwrite)
{
    System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(source);
    System.IO.DirectoryInfo outputDir = new System.IO.DirectoryInfo(destination);
    try
    {
        if ((inputDir.Exists))
        {
            if (!(outputDir.Exists))
            {
                createfolder(destination);
                // outputDir.Create();
            }
            //Get Each files and copy
            System.IO.FileInfo file = null;
            foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
            {
                file = eachfile;
                if ((overwrite))
                {
                    file.CopyTo(System.IO.Path.Combine(outputDir.FullName, file.Name), true);
                }
                else
                {
                    if (((System.IO.File.Exists(System.IO.Path.Combine(outputDir.FullName, file.Name))) == false))
                    {
                        file.CopyTo(System.IO.Path.Combine(outputDir.FullName, file.Name), false);
                    }
                }
                System.IO.File.Delete(file.FullName);
            }
            //Sub folder access code       
            System.IO.DirectoryInfo dir = null;
            foreach (System.IO.DirectoryInfo subfolderFile in inputDir.GetDirectories())
            {
                dir = subfolderFile;
                //Destination path                   
                if ((dir.FullName != outputDir.ToString()))
                {
                    MoveFiles(dir.FullName, System.IO.Path.Combine(outputDir.FullName, dir.Name), overwrite);
                }
                System.IO.Directory.Delete(dir.FullName);
            }
        }
    }
    catch (Exception ex)
    {

    }
}
Naisarg Parmar
  • 759
  • 8
  • 25