creating Virtual directory in IIS 7.0 using c#.net

后端 未结 2 1213
闹比i
闹比i 2021-01-03 05:17

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

can any one guide me with that

相关标签:
2条回答
  • 2021-01-03 05:47

    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!

    0 讨论(0)
  • 2021-01-03 05:52

    This page has pretty much everything you will need:

    http://forums.iis.net/t/1139885.aspx

    0 讨论(0)
提交回复
热议问题