Monday, March 3, 2008

create Virtual Directory using C#

http://www.vbforums.com/showthread.php?t=347207

Code to create Virtual Directory using C#

  1. Private Sub CreateVirtualDir(ByVal WebSite As String, ByVal AppName As String, ByVal Path As String)
  2. Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/Schema/AppIsolated")
  3. Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
  4. IISSchema.Dispose()
  5. If CanCreate Then
  6. Dim PathCreated As Boolean
  7. Try
  8. Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & WebSite & "/W3SVC/1/Root")
  9. 'make sure folder exists
  10. If Not System.IO.Directory.Exists(Path) Then
  11. System.IO.Directory.CreateDirectory(Path)
  12. PathCreated = True
  13. End If
  14. 'If the virtual directory already exists then delete it
  15. For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
  16. If VD.Name = AppName Then
  17. IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, AppName})
  18. IISAdmin.CommitChanges()
  19. Exit For
  20. End If
  21. Next VD
  22. 'Create and setup new virtual directory
  23. Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir")
  24. VDir.Properties("Path").Item(0) = Path
  25. VDir.Properties("AppFriendlyName").Item(0) = AppName
  26. VDir.Properties("EnableDirBrowsing").Item(0) = False
  27. VDir.Properties("AccessRead").Item(0) = True
  28. VDir.Properties("AccessExecute").Item(0) = True
  29. VDir.Properties("AccessWrite").Item(0) = False
  30. VDir.Properties("AccessScript").Item(0) = True
  31. VDir.Properties("AuthNTLM").Item(0) = True
  32. VDir.Properties("EnableDefaultDoc").Item(0) = True
  33. VDir.Properties("DefaultDoc").Item(0) = "default.htm,default.aspx,default.asp"
  34. VDir.Properties("AspEnableParentPaths").Item(0) = True
  35. VDir.CommitChanges()
  36. 'the following are acceptable params
  37. 'INPROC = 0
  38. 'OUTPROC = 1
  39. 'POOLED = 2
  40. VDir.Invoke("AppCreate", 1)
  41. Catch Ex As Exception
  42. If PathCreated Then
  43. System.IO.Directory.Delete(Path)
  44. End If
  45. Throw Ex
  46. End Try
  47. End If
  48. End Sub

No comments: