Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating Virtual directory in IIS 7.0 using c#.net

Tags:

.net

i am trying to create virtual directory in IIS 7.0 using c#.net or vb.net,

can any one guide me with that

like image 717
subash Avatar asked Dec 14 '22 01:12

subash


1 Answers

Subash,

As slugster says here, this page has all that you need: http://forums.iis.net/t/1139885.aspx

But if you're looking for code snippets, please find those below:

You can do this with the following batch file:

%systemroot%\system32\inetsrv\APPCMD add site /name:MyNewSite /bindings:"http/*:81:" /physicalPath:"C:\MyNewSite"
%systemroot%\system32\inetsrv\APPCMD set config "MyNewSite" /section:defaultDocument /enabled:true /+files.[value='home.html']

For the above snippet, please make sure that the directory exists, and that the web.config is a properly formatted xml document.

This can then be turned into a command console application in vb.net or c# by calling these commands as follows:

Module Module1

    Sub Main()
        Dim proc As Process = New Process()
        proc.StartInfo.FileName = "C:\Windows\system32\inetsrv\APPCMD"
        proc.StartInfo.Arguments = "add site /name:MyNewSite /bindings:""http/*:81:"" /physicalPath:""C:\MyNewSite"""
        proc.Start()
        proc.WaitForExit()
        proc.StartInfo.Arguments = "set config ""MyNewSite"" /section:defaultDocument /enabled:true /+files.[value='home.html']"
        proc.Start()
    End Sub

End Module

I hope this hlep,

Thanks!

EDIT: I found that you can do this directly with an API rather than executing external exes.

Also, please note that for installing virtual directories on IIS6 vs IIS7 is different when you use the APIs. First for installing with the IIS6, you need to do the following:

Imports System.DirectoryServices

Module Module1

    Sub Main()
        Dim deIIS As DirectoryEntry = New DirectoryEntry("IIS://" & Environment.MachineName & "/W3SVC/1/Root")
        Dim deNewVDir As DirectoryEntry = deIIS.Children.Add("MyNewSite", deIIS.SchemaClassName.ToString)
        deNewVDir.CommitChanges()
        deIIS.CommitChanges()
    End Sub

End Module

But in order to do this in IIS7, you need to do this (Please note that Microsoft.Web.Administration comes from C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll):

Imports Microsoft.Web.Administration

Module Module1

    Sub Main()
        Dim iisManager As New ServerManager
        Dim site As Site = iisManager.Sites.Add("MyNewSite", "http", "*:8080:", "C:\MyNewSite")
        Dim app As Microsoft.Web.Administration.Application = site.Applications.Add("/MyApp", "C:\MyNewSite")
        app.VirtualDirectories.Add("/VDir", "C:\MyNewSite")
        iisManager.CommitChanges()
    End Sub

End Module

I hope this is more helpful,

Thanks!

like image 90
Scott Avatar answered Dec 16 '22 16:12

Scott