locked
not able to delete directory in vb.net RRS feed

  • Question

  • User-1458744719 posted
     Dim filepathName As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/" & ID & "/")
                ' DeleteDirContents(New IO.DirectoryInfo("filepathName"))
                If Directory.Exists(filepathName) Then
                    Directory.Delete(filepathName)
                    ' Folder deleted message
                End If
    its giving me error directory is not empty
    how can i delete directory having files in it
    Wednesday, February 8, 2012 1:39 AM

Answers

All replies

  • User-1458744719 posted
    now below code is deleting sub directories but not the directory..........i also want to delete directory
    
    
    Dim filepathName As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/" & ID & "/")
    
                If Directory.Exists(filepathName) Then
                    Directory.Delete(filepathName, True)
                    ' Folder deleted message
                End If
    Wednesday, February 8, 2012 2:02 AM
  • User535082756 posted

    hi

    try this 

    
    

    string appPath = Request.PhysicalApplicationPath.ToString();
    DeleteDirectory(appPath + "dummy");

    public static void DeleteDirectory(string target_dir)
        {
            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);
    
            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }
    
            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }
    
            Directory.Delete(target_dir, false);
        }
    Wednesday, February 8, 2012 2:17 AM
  • User-1458744719 posted

    DeleteDirectory(appPath + "dummy");

    what is dummy here?????

    Wednesday, February 8, 2012 4:35 AM
  • User535082756 posted

    hi

    Dummy is the folder name...

    Wednesday, February 8, 2012 4:40 AM
  • User-1458744719 posted

    its doing the same thing deleteting images inside the folder but not deleting images

    Wednesday, February 8, 2012 4:46 AM
  • User1084963946 posted

    There is an example here:

    http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 10, 2012 11:13 AM