0

Alright so I'm having a bit of an issue here. I'm trying to delete a specific folder inside another folder on my webserver using ASP.NET (C#) The folder being deleted is based on a textbox.

The directory is like this

/images/folderx

folderx = txtDelFolder.Text;

The problem is that everything I try deletes every single thing inside the images folder. I'm guessing that it is not recognizing my folder in the filepath

string path = @"\httpdocs\images\ + txtDelFolder.Text;

I have also tried

string path = @"\httpdocs\images\ + txtDelFolder.Text + "\";

Tried all this with both single '\' and double '\'

Would appreciate any help on this

Also where it says <directfilepath> I actually have the filepath typed out, just didn't want to share that here.

****edit****

string path = Server.MapPath("~/imagestest/" + txtEditTitle.Text);

  if(Directory.Exists(path)) 
  { 
  DeleteDirectory(path); 
  } 
 } 
} 
private void DeleteDirectory(string path) 
{ 
 foreach(string filename in Directory.GetFiles(path)) 
 { 
 File.Delete(filename); 
 } 
 foreach(string subfolders in Directory.GetDirectories(path)) 
 { 
 Directory.Delete(subfolders, true); 
 } 
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MikeTech
  • 1
  • 1
  • 4
  • Use Server.MapPath() to get physical location of folders – malkam Jun 23 '14 at 16:43
  • @MikeTech Don't add code as a comment. It's completely unreadable. Format your original post and use the code formatting tools. – tnw Jun 23 '14 at 16:57

3 Answers3

2

Try this:

private void DeleteFiles(string folder)
        {
            string path=Server.MapPath("~/httpdocs/images/" + folder);
            string[] files=Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                File.Delete(file);
            }
             //then delete folder
              Directory.Delete(path);

        }
malkam
  • 2,337
  • 1
  • 14
  • 17
  • 1
    It's important to understand that whenever you delete a directory which is inside IIS Application IIS will unload your ApplicationDomain immediately (effectively re-staring your application). You might want to either use folders outside your application or remove only the files, leaving folders intact. Here is the link with a bit more details http://blogs.msdn.com/b/toddca/archive/2006/07/17/668412.aspx – Isantipov Jun 23 '14 at 17:06
  • Technically my directory is in the root of the site. Would this still have the same effect? – MikeTech Jun 23 '14 at 17:20
  • When I tried this it was still deleting files from /images/ and not from /images/folder/ – MikeTech Jun 23 '14 at 17:22
0

try this one :

public void DeleteFolder(string folderPath)
    {
        if (!Directory.Exists(folderPath))
            return;
        // get the directory with the specific name
        DirectoryInfo dir = new DirectoryInfo(folderPath);
        try
        {
            foreach (FileInfo fi in dir.GetFiles())
                fi.Delete();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
0

Don't see why this wouldn't work:

public static bool DeleteDirectory(string input)
{
    if (Directory.Exists(input))
    {
        Directory.Delete(input, true);
        return !Directory.Exists(input);
    }
    else
        return true;
}

string thePath = Server.MapPath(@"~/images/");
thePath = Path.Combine(Path.GetFullPath(thePath), txtInput.Text);

if(DeleteDirectory(thePath))
    Console.WriteLine("YAY");
else
    Console.WriteLine("BOO");
NickSuperb
  • 1,174
  • 1
  • 8
  • 28