我需要编写程序来创建一个IIS网站。是否有人可以向我展示如何编写此代码?
如有可能,请不要在针对IIS 7或以上版本时使用WMI(DirectoryEntry
)。在Microsoft.Web.Administration.dll
(windows\system32\inetsrv
)中有一个名为ServerManager
的API,使得这变得非常容易:
ServerManager serverManager = new ServerManager();
serverManager.Sites.Add("Mysite", "c:\temp\", 8080);
Imports System.DirectoryServices
Imports System
Public Class IISAdmin
Public Shared Function CreateWebsite(webserver As String, serverComment As String, serverBindings As String, homeDirectory As String) As Integer
Dim w3svc As DirectoryEntry
w3svc = New DirectoryEntry("IIS://localhost/w3svc")
'Create a website object array
Dim newsite() As Object
newsite = New Object(){serverComment, new Object(){serverBindings}, homeDirectory}
'invoke IIsWebService.CreateNewSite
Dim websiteId As Object
websiteId = w3svc.Invoke("CreateNewSite", newsite)
Return websiteId
End Function
Public Shared Sub Main(args As String())
Dim a As Integer
a = CreateWebsite("localhost", "Testing.com", ":80:Testing.com", "C:\\inetpub\\wwwroot")
Console.WriteLine("Created website with ID: " & a)
End Sub
End Class
public static int CreateWebsite(string webserver, string serverComment, string serverBindings, string homeDirectory)
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
//Create a website object array
object[] newsite = new object[]{serverComment, new object[]{serverBindings}, homeDirectory};
//invoke IIsWebService.CreateNewSite
object websiteId = (object)w3svc.Invoke("CreateNewSite", newsite);
return (int)websiteId;
}