参考代码
using System.DirectoryServices;   
protected System.DirectoryServices.DirectoryEntry dirroot;   
DirectoryEntry newVirDir = dirroot.Children.Add("Webtest","IIsWebVirtualDir");   
newVirDir.Invoke("AppCreate",true);   
newVirDir.CommitChanges();   
dirroot.CommitChanges();   
不知道有没有人能在电脑上试一下 然后给我个完整的代码

解决方案 »

  1.   


     /// <summary>
            /// 创建站点
            /// </summary>
            /// <param name="strPort"></param>
            /// <param name="strWebName"></param>
            /// <param name="strRootPath"></param>
            /// <returns>0 1 2 3 4</returns>
            public int CreateNewSite(string strPort, string strWebName, string strRootPath, string strDefault)
            {
                if (!IISAppPool(strWebName))
                    return 0;   //iis应用程序池创建失败
                if (ExistWebSite(strWebName, strPort) == "1")
                    return 1;   //网站名称重复
                else if (ExistWebSite(strWebName, strPort) == "2")
                    return 2;   //端口重复
                if (!Directory.Exists(strRootPath) || Directory.GetFiles(strRootPath).Length <= 0)
                    return 3;   //发布目录为空
    ////以上只是我需要的判断!!!
                string sID = GetNewWebSiteID();
                DirectoryEntry dirEntry = new DirectoryEntry(string.Format("IIS://{0}/W3SVC", hostName));
                DirectoryEntry child = dirEntry.Children.Add(sID, "IIsWebServer");//.Invoke("Create", "IIsWebServer", sID);
                //child.Invoke("AppCreate", true);
                child.Invoke("Put", "ServerBindings", ":" + strPort + ":");
                child.Invoke("Put", "ServerState", 2);
                child.Invoke("Put", "DefaultDoc", string.IsNullOrEmpty(strDefault) ? "Default.aspx,Index.aspx" : strDefault.Replace(",", ",").Trim(new char[] { ',' }));
                child.Invoke("Put", "ServerAutoStart", true);
                child.Invoke("Put", "ServerComment", strWebName + " " + strPort);
                child.Properties["ScriptMaps"].Value = ScriptArray();
                child.Invoke("SetInfo");            DirectoryEntry virDir = child.Children.Add("Root", "IISWebVirtualDir");
                virDir.Invoke("AppCreate", true);
                virDir.CommitChanges();
                child.CommitChanges();            virDir.Properties["Path"][0] = strRootPath;
                // AccessFlags
                // 1. 虚拟目录|应用程序设置|执行权限为“无”时
                // (1)脚本资源访问
                // 不可读取且不可写入为16;可读取不可写入为17;
                // 可写入不可读取为18;可读取且可写入为19
                // (2)无脚本资源访问
                // 不可读取且不可写入为0;可读取不可写入为1;
                // 可写入不可读取为2;可读取且可写入为3            // 2. 虚拟目录|应用程序设置|执行权限为“纯脚本”时
                // (1)脚本资源访问
                // 不可读取且不可写入为528;可读取不可写入为529;
                // 可写入不可读取为530;可读取且可写入为531
                // (2)无脚本资源访问
                // 不可读取且不可写入为512;可读取不可写入为513;
                // 可写入不可读取为514;可读取且可写入为515            // 3. 虚拟目录|应用程序设置|执行权限为“脚本和可执行文件”时
                // (1)脚本资源访问
                // 不可读取且不可写入为532;可读取不可写入为533;
                // 可写入不可读取为534;可读取且可写入为535
                // (2)无脚本资源访问
                // 不可读取且不可写入为516;可读取不可写入为517;
                // 可写入不可读取为518;可读取且可写入为519
                virDir.Properties["AccessFlags"][0] = 513;
                virDir.Properties["FrontPageWeb"][0] = 1;
                virDir.Properties["AppRoot"][0] = string.Format("/{0}/W3SVC/{1}/Root", hostName, sID);
                //virDir.Properties["AppFriendlyName"][0] = "默认应用程序";
                virDir.Properties["AspEnableParentPaths"][0] = true;
                //virDir.Properties["AppPoolId"].Value = strWebName; //"DefaultAppPool";//
                virDir.Properties["AuthAnonymous"][0] = true; // 允许匿名访问
                //virDir.Invoke("Appcreate3", new object[] { 0, strWebName, true });            virDir.CommitChanges();
                child.CommitChanges();
                dirEntry.CommitChanges();
                child.Invoke("Start", new object[] { });            return 4;
            }
            /// <summary>
            /// 获取一个新的网站编号
            /// </summary>
            /// <returns></returns>
            public string GetNewWebSiteID()
            {
                int i = 1;
                DirectoryEntry dirEntry = new DirectoryEntry(string.Format("IIS://{0}/W3SVC", hostName));
                foreach (DirectoryEntry child in dirEntry.Children)
                    if (child.SchemaClassName == "IIsWebServer")
                        try
                        {
                            if (Convert.ToInt32(child.Name) >= i)
                                i = Convert.ToInt32(child.Name) + 1;
                        }
                        catch { }
                return i.ToString();
            }
      

  2.   

    C# IIS操作
    基本上都有了
      

  3.   

    3楼辛苦了 我用了你的方法还是有点问题  
    我想请问一下 DirectoryEntry dirEntry = new DirectoryEntry(string.Format("IIS://{0}/W3SVC", hostname));这个hostname如果是在自己的电脑上做测试是不是就是自己的电脑名称呢
    还是。。
      

  4.   

    3楼辛苦了 我用了你的方法还是有点问题   
    我想请问一下 DirectoryEntry dirEntry = new DirectoryEntry(string.Format("IIS://{0}/W3SVC", hostname));这个hostname如果是在自己的电脑上做测试是不是就是自己的电脑名称呢
    还是。。