Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / Visual Basic

Managing virtual directories on multiple IIS sites and servers

Rate me:
Please Sign up or sign in to vote.
3.74/5 (11 votes)
25 May 20053 min read 67.5K   481   30  
A simple tool to check virtual directories settings on multiple web sites and web servers.
Imports System.DirectoryServices

Public Class ChooserForm
  Inherits System.Windows.Forms.Form

#Region " Codice generato da Progettazione Windows Form "

  Public Sub New()
    MyBase.New()

    'Chiamata richiesta da Progettazione Windows Form.
    InitializeComponent()

    'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()

  End Sub

  'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Richiesto da Progettazione Windows Form
  Private components As System.ComponentModel.IContainer

  'NOTA: la procedura che segue � richiesta da Progettazione Windows Form.
  'Pu� essere modificata in Progettazione Windows Form.  
  'Non modificarla nell'editor del codice.
  Friend WithEvents btnCancel As System.Windows.Forms.Button
  Friend WithEvents btnOk As System.Windows.Forms.Button
  Friend WithEvents dgOptions As System.Windows.Forms.DataGrid
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents txtFilter As System.Windows.Forms.TextBox
  Friend WithEvents btnApply As System.Windows.Forms.Button
  Friend WithEvents btnDesel As System.Windows.Forms.Button
  Friend WithEvents btnSel As System.Windows.Forms.Button
  Friend WithEvents DataGridTableStyle1 As System.Windows.Forms.DataGridTableStyle
  Friend WithEvents DataGridBoolColumn1 As System.Windows.Forms.DataGridBoolColumn
  Friend WithEvents DataGridTextBoxColumn1 As System.Windows.Forms.DataGridTextBoxColumn
  Friend WithEvents DataGridTextBoxColumn2 As System.Windows.Forms.DataGridTextBoxColumn
  Friend WithEvents DataGridTableStyle2 As System.Windows.Forms.DataGridTableStyle
  Friend WithEvents DataGridBoolColumn2 As System.Windows.Forms.DataGridBoolColumn
  Friend WithEvents DataGridTextBoxColumn3 As System.Windows.Forms.DataGridTextBoxColumn
  Friend WithEvents DataGridTextBoxColumn4 As System.Windows.Forms.DataGridTextBoxColumn
  Friend WithEvents btnReloadWebSites As System.Windows.Forms.Button
  Friend WithEvents btnServerChooser As System.Windows.Forms.Button
  Friend WithEvents chkWebSiteNameFirst As System.Windows.Forms.CheckBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ChooserForm))
    Me.dgOptions = New System.Windows.Forms.DataGrid
    Me.DataGridTableStyle1 = New System.Windows.Forms.DataGridTableStyle
    Me.DataGridBoolColumn1 = New System.Windows.Forms.DataGridBoolColumn
    Me.DataGridTextBoxColumn1 = New System.Windows.Forms.DataGridTextBoxColumn
    Me.DataGridTextBoxColumn2 = New System.Windows.Forms.DataGridTextBoxColumn
    Me.DataGridTableStyle2 = New System.Windows.Forms.DataGridTableStyle
    Me.DataGridBoolColumn2 = New System.Windows.Forms.DataGridBoolColumn
    Me.DataGridTextBoxColumn3 = New System.Windows.Forms.DataGridTextBoxColumn
    Me.DataGridTextBoxColumn4 = New System.Windows.Forms.DataGridTextBoxColumn
    Me.btnCancel = New System.Windows.Forms.Button
    Me.btnOk = New System.Windows.Forms.Button
    Me.Label1 = New System.Windows.Forms.Label
    Me.txtFilter = New System.Windows.Forms.TextBox
    Me.btnApply = New System.Windows.Forms.Button
    Me.btnDesel = New System.Windows.Forms.Button
    Me.btnSel = New System.Windows.Forms.Button
    Me.btnReloadWebSites = New System.Windows.Forms.Button
    Me.btnServerChooser = New System.Windows.Forms.Button
    Me.chkWebSiteNameFirst = New System.Windows.Forms.CheckBox
    CType(Me.dgOptions, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'dgOptions
    '
    Me.dgOptions.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.dgOptions.DataMember = ""
    Me.dgOptions.HeaderForeColor = System.Drawing.SystemColors.ControlText
    Me.dgOptions.Location = New System.Drawing.Point(8, 76)
    Me.dgOptions.Name = "dgOptions"
    Me.dgOptions.Size = New System.Drawing.Size(752, 292)
    Me.dgOptions.TabIndex = 0
    Me.dgOptions.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {Me.DataGridTableStyle1, Me.DataGridTableStyle2})
    '
    'DataGridTableStyle1
    '
    Me.DataGridTableStyle1.DataGrid = Me.dgOptions
    Me.DataGridTableStyle1.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {Me.DataGridBoolColumn1, Me.DataGridTextBoxColumn1, Me.DataGridTextBoxColumn2})
    Me.DataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText
    Me.DataGridTableStyle1.MappingName = "WebSiteList"
    '
    'DataGridBoolColumn1
    '
    Me.DataGridBoolColumn1.AllowNull = False
    Me.DataGridBoolColumn1.FalseValue = False
    Me.DataGridBoolColumn1.HeaderText = "Selected"
    Me.DataGridBoolColumn1.MappingName = "Selected"
    Me.DataGridBoolColumn1.NullValue = CType(resources.GetObject("DataGridBoolColumn1.NullValue"), Object)
    Me.DataGridBoolColumn1.TrueValue = True
    Me.DataGridBoolColumn1.Width = 75
    '
    'DataGridTextBoxColumn1
    '
    Me.DataGridTextBoxColumn1.Format = ""
    Me.DataGridTextBoxColumn1.FormatInfo = Nothing
    Me.DataGridTextBoxColumn1.HeaderText = "MetabasePath"
    Me.DataGridTextBoxColumn1.MappingName = "MetabasePath"
    Me.DataGridTextBoxColumn1.Width = 250
    '
    'DataGridTextBoxColumn2
    '
    Me.DataGridTextBoxColumn2.Format = ""
    Me.DataGridTextBoxColumn2.FormatInfo = Nothing
    Me.DataGridTextBoxColumn2.HeaderText = "WebSiteName"
    Me.DataGridTextBoxColumn2.MappingName = "WebSiteName"
    Me.DataGridTextBoxColumn2.Width = 400
    '
    'DataGridTableStyle2
    '
    Me.DataGridTableStyle2.DataGrid = Me.dgOptions
    Me.DataGridTableStyle2.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {Me.DataGridBoolColumn2, Me.DataGridTextBoxColumn3, Me.DataGridTextBoxColumn4})
    Me.DataGridTableStyle2.HeaderForeColor = System.Drawing.SystemColors.ControlText
    Me.DataGridTableStyle2.MappingName = "ServerList"
    '
    'DataGridBoolColumn2
    '
    Me.DataGridBoolColumn2.AllowNull = False
    Me.DataGridBoolColumn2.FalseValue = False
    Me.DataGridBoolColumn2.HeaderText = "Selected"
    Me.DataGridBoolColumn2.MappingName = "Selected"
    Me.DataGridBoolColumn2.NullValue = CType(resources.GetObject("DataGridBoolColumn2.NullValue"), Object)
    Me.DataGridBoolColumn2.TrueValue = True
    Me.DataGridBoolColumn2.Width = 75
    '
    'DataGridTextBoxColumn3
    '
    Me.DataGridTextBoxColumn3.Format = ""
    Me.DataGridTextBoxColumn3.FormatInfo = Nothing
    Me.DataGridTextBoxColumn3.HeaderText = "ServerName"
    Me.DataGridTextBoxColumn3.MappingName = "ServerName"
    Me.DataGridTextBoxColumn3.Width = 150
    '
    'DataGridTextBoxColumn4
    '
    Me.DataGridTextBoxColumn4.Format = ""
    Me.DataGridTextBoxColumn4.FormatInfo = Nothing
    Me.DataGridTextBoxColumn4.HeaderText = "Description"
    Me.DataGridTextBoxColumn4.MappingName = "Description"
    Me.DataGridTextBoxColumn4.Width = 250
    '
    'btnCancel
    '
    Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btnCancel.Location = New System.Drawing.Point(604, 372)
    Me.btnCancel.Name = "btnCancel"
    Me.btnCancel.TabIndex = 1
    Me.btnCancel.Text = "Cancel"
    '
    'btnOk
    '
    Me.btnOk.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btnOk.Location = New System.Drawing.Point(684, 372)
    Me.btnOk.Name = "btnOk"
    Me.btnOk.TabIndex = 2
    Me.btnOk.Text = "Okay"
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 44)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 3
    Me.Label1.Text = "Filter clause:"
    '
    'txtFilter
    '
    Me.txtFilter.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.txtFilter.Location = New System.Drawing.Point(76, 40)
    Me.txtFilter.Name = "txtFilter"
    Me.txtFilter.Size = New System.Drawing.Size(612, 20)
    Me.txtFilter.TabIndex = 4
    Me.txtFilter.Text = ""
    '
    'btnApply
    '
    Me.btnApply.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btnApply.Location = New System.Drawing.Point(696, 40)
    Me.btnApply.Name = "btnApply"
    Me.btnApply.Size = New System.Drawing.Size(60, 23)
    Me.btnApply.TabIndex = 5
    Me.btnApply.Text = "Apply"
    '
    'btnDesel
    '
    Me.btnDesel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
    Me.btnDesel.Location = New System.Drawing.Point(128, 372)
    Me.btnDesel.Name = "btnDesel"
    Me.btnDesel.Size = New System.Drawing.Size(112, 23)
    Me.btnDesel.TabIndex = 6
    Me.btnDesel.Text = "Deselect shown"
    '
    'btnSel
    '
    Me.btnSel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
    Me.btnSel.Location = New System.Drawing.Point(8, 372)
    Me.btnSel.Name = "btnSel"
    Me.btnSel.Size = New System.Drawing.Size(112, 23)
    Me.btnSel.TabIndex = 7
    Me.btnSel.Text = "Select shown"
    '
    'btnReloadWebSites
    '
    Me.btnReloadWebSites.Location = New System.Drawing.Point(12, 8)
    Me.btnReloadWebSites.Name = "btnReloadWebSites"
    Me.btnReloadWebSites.Size = New System.Drawing.Size(168, 23)
    Me.btnReloadWebSites.TabIndex = 10
    Me.btnReloadWebSites.Text = "Reload web sites list"
    '
    'btnServerChooser
    '
    Me.btnServerChooser.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btnServerChooser.Location = New System.Drawing.Point(680, 8)
    Me.btnServerChooser.Name = "btnServerChooser"
    Me.btnServerChooser.Size = New System.Drawing.Size(76, 23)
    Me.btnServerChooser.TabIndex = 12
    Me.btnServerChooser.Text = "Servers..."
    '
    'chkWebSiteNameFirst
    '
    Me.chkWebSiteNameFirst.Checked = True
    Me.chkWebSiteNameFirst.CheckState = System.Windows.Forms.CheckState.Checked
    Me.chkWebSiteNameFirst.Location = New System.Drawing.Point(184, 8)
    Me.chkWebSiteNameFirst.Name = "chkWebSiteNameFirst"
    Me.chkWebSiteNameFirst.Size = New System.Drawing.Size(272, 24)
    Me.chkWebSiteNameFirst.TabIndex = 13
    Me.chkWebSiteNameFirst.Text = "Naming convention: ""WebSite - SebServer"""
    '
    'ChooserForm
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(764, 401)
    Me.Controls.Add(Me.chkWebSiteNameFirst)
    Me.Controls.Add(Me.txtFilter)
    Me.Controls.Add(Me.btnServerChooser)
    Me.Controls.Add(Me.btnReloadWebSites)
    Me.Controls.Add(Me.btnSel)
    Me.Controls.Add(Me.btnDesel)
    Me.Controls.Add(Me.btnApply)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.btnOk)
    Me.Controls.Add(Me.btnCancel)
    Me.Controls.Add(Me.dgOptions)
    Me.MinimumSize = New System.Drawing.Size(772, 428)
    Me.Name = "ChooserForm"
    Me.ShowInTaskbar = False
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
    Me.Text = "Chooser Form"
    CType(Me.dgOptions, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)

  End Sub

#End Region

  Public ShownTable As String                 ' The name of the DataTable to be shown
  Public DefaultFilterExpression As String    ' Default filter clause

  Dim dsOptions As DataSet                    ' DataSet reflecting the configuration file

  Private Sub ChooserForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txtFilter.Text = DefaultFilterExpression

    ' Load configuration file
    dsOptions = New DataSet
    dsOptions.ReadXml(Application.StartupPath & "\VDirViewerOptions.xml", XmlReadMode.ReadSchema)
    dgOptions.DataSource = dsOptions.Tables(ShownTable).DefaultView

    ' Deactivate options for the "Web Site Chooser" only
    If ShownTable <> "WebSiteList" Then
      btnReloadWebSites.Visible = False
      chkWebSiteNameFirst.Visible = False
      btnServerChooser.Visible = False
    End If

  End Sub

  ' Close the chooser form, discarding last changes
  Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
  End Sub

  ' Close the chooser form, updating the configuration file
  Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    dsOptions.WriteXml(Application.StartupPath & "\VDirViewerOptions.xml", XmlWriteMode.WriteSchema)
    Me.Close()
  End Sub

  ' Apply the filtering query
  Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
    CType(dgOptions.DataSource, DataView).RowFilter = txtFilter.Text
  End Sub

  Private Sub btnSel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSel.Click
    ' Make selected all the shown entries
    Dim dv As DataView = CType(dgOptions.DataSource, DataView)
    Dim i As Integer
    For i = 0 To dv.Count - 1
      dv(i)("Selected") = True
    Next
    RefreshGrid()
  End Sub

  Private Sub btnDesel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDesel.Click
    ' Make deselected all the shown entries
    Dim dv As DataView = CType(dgOptions.DataSource, DataView)
    Dim i As Integer
    For i = 0 To dv.Count - 1
      dv(i)("Selected") = False
    Next
    RefreshGrid()
  End Sub

  Private Sub RefreshGrid()
    ' Workaround for a visualization bug of the grid
    Dim dv As DataView = CType(dgOptions.DataSource, DataView)
    dgOptions.DataSource = Nothing
    dgOptions.DataSource = dv
  End Sub

  Private Sub btnReloadWebSites_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReloadWebSites.Click
    Me.Cursor.Current = Cursors.WaitCursor
    Dim NoEntrySelected As Boolean = True

    ' Loop on selected Servers and retrieves hosted web sites on each
    Dim websiteList As DataTable = dsOptions.Tables("WebSiteList")
    websiteList.Clear()
    Dim dt As DataTable = dsOptions.Tables("ServerList")
    Dim dr As DataRow
    For Each dr In dt.Rows
      If dr("Selected") = True Then
        NoEntrySelected = False
        Try
          ScanServerWebSites(dr("ServerName"), websiteList, chkWebSiteNameFirst.Checked)
        Catch ex As Exception
          MessageBox.Show(ex.Message)
        End Try
      End If
    Next
    Me.Cursor.Current = Cursors.Default
    If NoEntrySelected Then
      MessageBox.Show("You have to select at least one Server from the Servers list.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
  End Sub

  ' Retrieves all the web sites hosted by the specified web server
  Private Sub ScanServerWebSites(ByVal serverName As String, ByVal websiteList As DataTable, Optional ByVal WebSiteNameFirst As Boolean = True)
    Dim ScanPath As String = "IIS://" & serverName & "/W3SVC"
    Dim site As New DirectoryEntry(ScanPath)
    Dim CName As String = site.SchemaClassName.ToString()
    If CName = "IIsWebService" Then
      Dim VDirs As DirectoryEntries = site.Children
      Dim curVDir As DirectoryEntry
      For Each curVDir In VDirs
        Try
          Dim dr As DataRow = websiteList.NewRow
          dr("Selected") = False
          If WebSiteNameFirst Then
            dr("WebSiteName") = curVDir.Properties("ServerComment").Value & " - " & serverName
          Else
            dr("WebSiteName") = serverName & " - " & curVDir.Properties("ServerComment").Value
          End If
          dr("MetabasePath") = curVDir.Path
          websiteList.Rows.Add(dr)
        Catch
        End Try
      Next
    End If
  End Sub

  ' Open the "Server Chooser" form
  Private Sub btnServerChooser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnServerChooser.Click
    ' Persist configuration file (for the eventually modified web sites list)
    dsOptions.WriteXml(Application.StartupPath & "\VDirViewerOptions.xml", XmlWriteMode.WriteSchema)

    Dim f As New ChooserForm
    f.Text = "Server Chooser"
    f.ShownTable = "ServerList"
    f.DefaultFilterExpression = "ServerName LIKE '*'"
    f.ShowDialog()

    ' Re-load configuration file (eventually modified by the "Server Chooser")
    dsOptions = New DataSet
    dsOptions.ReadXml(Application.StartupPath & "\VDirViewerOptions.xml", XmlReadMode.ReadSchema)
    dgOptions.DataSource = dsOptions.Tables(ShownTable).DefaultView
  End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions