title: How to Delete a Virtual Directory from an FTP Site in IIS 7 and IIS 7.5 using C#/VB.Net and WMI?
body: <p>I hope everybody is doing fine.</p>
  
  <p>I try to delete a virtual directory using WMi (Server Manager Class) and recreate with different values. 
  <strong>The problem I am facing is that the virtual directory is not getting deleted.</strong> Please help. </p>
  
  <p><strong>How to check existence of a virtual dir and the call delete?</strong> </p>
  
  <p>and similarly how check whether a directory exists before adding the same again with slightly different properties.</p>
  
  <blockquote>
    <p>Basically the goal was to rename a
    virtual directory along with renaming
    a website. I hope I am clear</p>
  </blockquote>
  
  <p>Here is my code.</p>
  
  <pre><code>Try
    Using mgr As New ServerManager()
      Dim site As Site = mgr.Sites(DomainName)
      Dim app As Application = site.Applications("/") '.CreateElement() '("/" &amp; VirDirName)
      Dim VirDir As VirtualDirectory = app.VirtualDirectories.CreateElement()
           For Each VirDir In app.VirtualDirectories
                If VirDir("path") = "/" &amp; VirDirName Then
                      app.VirtualDirectories.Remove(VirDir)
                      Exit For
                End If
           Next
           mgr.CommitChanges()
    End Using
   Catch Err As Exception
        Ex = Err
        Throw New Exception(Err.Message, Ex)
   End Try
  </code></pre>