请问怎么用c#程序设置iis新建站点,c#程序设置dns二级域名主机,然后把刚才的主机加到新建的站点上。
给个思路,或给段的代码。
我是2003操作系统。
小弟先谢了!

解决方案 »

  1.   

    http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx
      

  2.   

    http://www.codeguru.com/Csharp/.NET/net_general/article.php/c4603/
      

  3.   

    创建虚拟目录  
      
     DirectoryEntry是.Net给我们的一大礼物,他的名字我们就知道他的功能--目录入口。使用过ADSI的人都知道操作IIS,WinNT这些时,我们还需要提供他们的Path,操作IIS时,这个Path的格式为:  
      
     IIS://ComputerName/Service/Website/Directory  
      
     ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost  
     Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"  
     WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。  
     Directory:不用说,即操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)。  
     首先我们获取一个站点的顶层目录(根目录):  
      
     DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");  
      
     如果我们创建这个对象是没有发生异常,则表示这个目录是真实存在的。  
      
     下面我们来添加新的虚拟目录,比如我们要加的是"Aspcn":  
      
     DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir");  
     newVirDir.Invoke("AppCreate",true);  
     newVirDir.CommitChanges();  
     rootfolder.CommitChanges();  
        
      
     创建目录的思路很简单,即在根目录的子集(rootfolder.Children)中再添加一条记录,这里使用的是DirectoryEntries类中的Add方法,它返回的是一个DirectoryEntry,表示新加入的目录,第一个参数是虚拟目录的名字,第二个则是Schema的类名以表明我们加入的目录类型。然后再使用DirectoryEntry的Invoke方法,调用ADSI中的"AppCreate"方法将目录真正创建(似乎不走这一步也可以创建目录成功,但是为了保险起见,大家还是用吧),最后便是依次调用新、根目录的CommitChanges方法,确认此次操作。  
      
     在创建新目录时,我们也可以同时给这个目录的属性赋值,但是我的实战经验告诉我,最好不要这样做,如果创建时就赋值,将有很多属性不能赋值成功,比如重要的表示真实目录的Path属性。因此飞刀建议大家最好是先创建目录,然后再赋值,即更新目录信息。  
      
     更新虚拟目录  
      
     相信大家对IIS都比较熟悉,了解IIS中一些重要的设置,如可读(AccessRead)、可写(AccessWrite)、可执行(AccessExecute)等。这些都可通过对DirectoryEntry的Properties属性集合的赋值来实现。赋值可以通过两种方式来完成:  
      
     第一种是调用Properties集合的Add方法,如:  
      
     dir.Properties["AccessRead"].Add(true);  
      
     第二种是对第一个索引值赋值:  
      
     dir.Properties["AccessRead"][0] = true;  
      
     这两种方法都是可行的。具体是要看你的喜好了。  
      
     在进行赋值之前我们还是要确定要要赋值的目标吧:)这里我们使用DirectoryEntries类的Find方法,如:  
      
     DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");  
      
     找到了,我们就可以赋值了。赋值时一定要好好看看啊,虚拟目录的属性值可以超多,一查一大堆。。:(太多了,飞刀我也不重复了,大家去微软的站点上查:)  
      
     比较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path  
      
     删除虚拟目录  
      
     删除虚拟目录的方法也很简单,就是找到你要删除的虚拟目录,然后调用AppDelete方法。  
      
     DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");  
     de.Invoke("AppDelete",true);  
     rootfolder.CommitChanges();  
        
      
     还有一种方法,就是调用Root目录的Delete方法。  
      
     object[] paras = new object[2];  
     paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录  
     paras[1] = "Aspcn";  
     rootfolder.Invoke("Delete",paras);  
     rootfolder.CommitChanges();
      

  4.   

    wangxy0919() 行 29:  foreach(DirectoryEntry e in root.Children)
    行 30:  {
    行 31:  if(e.SchemaClassName == "IIsWebServer")
    System.Runtime.InteropServices.COMException: 拒绝访问。为什么我总是在 29行报错啊!是不是我的路径有问题?
    DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
      

  5.   

    可能权限问题,我试过也有这样的问题,不知道怎么解决。:)
    后来我改用vbs来设置iis的
      

  6.   

    wangxy0919()  
    怎么用vbs来设置说来听听啊!
      

  7.   

    参考http://dev.csdn.net/develop/article/19/19548.shtm
    将上面的脚本保存为vbs后缀文件
    我是在安装时设置iis,做了些改动
    调用的时候用process.start("wscript.exe","….vbs")
      

  8.   

    据我所知,那是因为必须是IIS 5.1 以上的版本才能使用那些属性。         /// <summary>
    /// 
    /// </summary>
    public class ConfigIIS
    {
            #region Function CreateWebSite
            /// <summary>
            /// This function can work, but do not use it. It'll be modified in future.
            /// </summary>
            /// <param name="webSiteName"></param>
            /// <param name="pathToRoot"></param>
            /// <returns></returns>
            public static 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.Invoke("AppCreate3", new object[] {2, "DefaultAppPool", true}); 
                siteVDir.CommitChanges();
                site.CommitChanges();
                return siteID;
            }
            #endregion        /// <summary>
            /// Create a virtual directory on default web site, and create an application.
            /// </summary>
            /// <param name="siteName"></param>
            /// <param name="appName">Generally same as siteName</param>
            /// <param name="inPath">Physical path</param>
            public static void CreateVDirInDefaultWebSite(string siteName, string appName, string inPath)
            {
                // The default web site
                DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
     
                DirectoryEntry newVDir = root.Children.Add(siteName, "IIsWebVirtualDir");
                newVDir.Properties["Path"][0] = inPath;
                newVDir.Properties["AccessFlags"][0] = 513;
                newVDir.Properties["FrontPageWeb"][0] = 1;
                newVDir.Properties["AppFriendlyName"][0] = appName;
                newVDir.CommitChanges();
                root.CommitChanges();            // Create an Application
                newVDir.Invoke("AppCreate", true);            newVDir.CommitChanges();
                root.CommitChanges();
            }        private static void CreatePhysicalDirectory(string path, string fileName)
            {
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                    System.IO.StreamWriter file = new System.IO.StreamWriter(path + "\\" + fileName, false, System.Text.Encoding.Default);
                    file.Write("<html><body><br><br><br><br><center><h2><font name='Verdana'>Welcome</font></font></h2></center></body></html>");
                    file.Close();
                }
            }        #region Not supplied on IIS 5.1 and earlier
            // IIS 5.1 and earlier: IIsApplicationPools is not available.
            private static void CreateAppPool(string appPoolName)
            {
                DirectoryEntry newpool;
                DirectoryEntry apppools=new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                try 
                {
                    newpool=apppools.Children.Add(appPoolName, "IIsApplicationPool");
                    newpool.CommitChanges();
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    Console.WriteLine(ex.Message);
                }                
            }        // IIS 5.1 and earlier: IIsApplicationPools is not available.
            private static void AssignAppPool(DirectoryEntry newvdir, string appPoolName)
            {
                object[] param={0, appPoolName, true};
                newvdir.Invoke("AppCreate3", param);
            }
            #endregion
    }
    上面是我现在这个工程里头所使用的,不完善,很多都需要改。
    在Microsoft的msdn online里头有不少这方面的例子,但很多都在2000的IIS中用不了,版本太低了。
      

  9.   

    http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx
    参考以上地址