要写段程序,实现在代码中创建iis站点,并进行一些配置,达到不用操作iis就可以建立网站的功能.
参考了 孟子E章 的一篇文章 http://blog.csdn.net/net_lover/archive/2007/08/26/1759880.aspx
的确可以实现创建站点功能,但是由于我要创建的站点指定的目录下有aspx文件,是要创建应用程序才可以执行aspx的.可此方法执行后的确是建立了应用程序,在应用程序池中也看得到此次创建应用程序的名称,但就是无法浏览aspx页面,(htm可以浏览),一定要在iis中此站点的 属性 对话框中,在 主目录 面版中手动删除此应用程序,再点击创建按纽,然后才可以正常访问.
代码如下:          String[] ScriptMapsList = new string[53];
            ScriptMapsList[0] = @".asp,C:\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE";
//省略一些扩展名的绑定信息,
            ScriptMapsList[52] = @".refresh,c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG";
            ///注意:这里没有进行进行端口存在的检查
            // Access Flags
            const int MD_ACCESS_READ = 0x00000001; //Allow read access.
            const int MD_ACCESS_SCRIPT = 0x00000200; // Allow script execution.
            string entPath = "IIS://localhost/w3svc";
            System.DirectoryServices.DirectoryEntry rootEntry = new System.DirectoryServices.DirectoryEntry(entPath);
            int siteID = 1;
            //得到现有的站点标识
            foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
            {
                if (entry.SchemaClassName == "IIsWebServer")
                {
                    int ID = Convert.ToInt32(entry.Name);                    if (ID >= siteID)
                    {
                        siteID = ID + 1;
                    }
                }
            }            //设置端口号等信息
            System.DirectoryServices.DirectoryEntry newSiteEntry = rootEntry.Children.Add(siteID.ToString(), "IIsWebServer");
            newSiteEntry.Properties["ServerBindings"].Value = ":8030:";
            newSiteEntry.Properties["ServerComment"].Value = "mengxianhui2";
            newSiteEntry.CommitChanges();            System.DirectoryServices.DirectoryEntry vDirEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
            vDirEntry.Properties["Path"].Value = @"C:\load";
   
            vDirEntry.Invoke("AppCreate", true);
            vDirEntry.Properties["ScriptMaps"].Value = ScriptMapsList;
            vDirEntry.Properties["AppFriendlyName"][0] = "默认应用程序";
            vDirEntry.Properties["AppIsolated"][0] = 2;
            vDirEntry.Properties["AccessFlags"][0] = MD_ACCESS_READ | MD_ACCESS_SCRIPT;
            vDirEntry.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
            vDirEntry.CommitChanges();
            newSiteEntry.CommitChanges();            MessageBox.Show("创建完成!");不知道其中哪里要做修改,使直接执行此段代码后就可以访问,而不用手工在iis中操作.
谢谢。..

解决方案 »

  1.   

    http://www.cnblogs.com/dlwang2002/archive/2005/05/25/207968.html
    看看,有没有用.
      

  2.   

    不行啊.用了以下的方法/// <summary>
    /// 生成一个新的站点
    /// </summary>
    /// <param name="webSiteName">站点名称</param>
    /// <param name="pathToRoot">物理地址</param>
    /// <returns></returns>
    public int CreateWebSite(string webSiteName, string pathToRoot)
    {
    DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
    // Find unused ID value for new web site
    int siteID = 1;
    foreach(DirectoryEntry e in root.Children)
    {
    if(e.SchemaClassName == "IIsWebServer")
    {
    int ID = Convert.ToInt32(e.Name);
    if(ID >= siteID)
    {
    siteID = ID+1;
    }
    }
    }
    // Create web site
    DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
    site.Invoke("Put", "ServerComment", webSiteName);
    site.Invoke("Put", "KeyType", "IIsWebServer");
    site.Invoke("Put", "ServerBindings", ":80:");
    site.Invoke("Put", "ServerState", 2);
    site.Invoke("Put", "FrontPageWeb", 1);
    site.Invoke("Put", "DefaultDoc", "Default.aspx");
    site.Invoke("Put", "SecureBindings", ":443:");
    site.Invoke("Put", "ServerAutoStart", 1);
    site.Invoke("Put", "ServerSize", 1);
    site.Invoke("SetInfo");
    // Create application virtual directory
    DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
    siteVDir.Properties["AppIsolated"][0] = 2;
    siteVDir.Properties["Path"][0] = pathToRoot;
    siteVDir.Properties["AccessFlags"][0] = 513;
    siteVDir.Properties["FrontPageWeb"][0] = 1;
    siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root";
    siteVDir.Properties["AppFriendlyName"][0] = "Root";
    siteVDir.CommitChanges();
    site.CommitChanges();
    return siteID;
    }结果是一样的.还是要在iis中手动删除应用程序,再创建,然后才可以正常访问.
            
      

  3.   

    以下方法可以了                    //得到现有的站点标识
                        foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
                        {
                            if (entry.SchemaClassName == "IIsWebServer")
                            {
                                siteID = Convert.ToInt32(entry.Name);
                                break;
                            }
                        }                    if (siteID == -1)
                        {
                            MessageBox.Show(this, "当前没有默认站点,请新建站点", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                            return;
                        }                    //得到当前默认
                        System.DirectoryServices.DirectoryEntry defaultSiteEntry = rootEntry.Children.Find(siteID.ToString(), "IIsWebServer");
                        System.DirectoryServices.DirectoryEntry vDirEntry = defaultSiteEntry.Children.Find("root", "IIsWebVirtualDir");                    vDirEntry.Properties["Path"][0] = I_RootPath;
                        vDirEntry.Invoke("AppCreate", true);
                        vDirEntry.Properties["ScriptMaps"].Value = ScriptMapsList;
                        vDirEntry.Properties["AccessRead"][0] = true;
                        vDirEntry.Properties["ContentIndexed"][0] = false;
                        vDirEntry.Properties["AppFriendlyName"][0] = "";
                        vDirEntry.Properties["AuthFlags"][0] = 1;
                        vDirEntry.Properties["AppIsolated"][0] = 2;
                        vDirEntry.Properties["AccessScript"][0] = true;
                        vDirEntry.Properties["DontLog"][0] = true;                    vDirEntry.CommitChanges();
                        defaultSiteEntry.CommitChanges();