首页 > 网站 > IIS > 正文

用ADSI控制IIS创建网站, 虚拟目录…… (C#)

2024-08-29 03:13:54
字体:
来源:转载
供稿:网友

 这是当年赶工的网站用到的东东, 结果快做完了警察叔叔来个网站要备案,朋友不干了,我也停工了,

翻出来共享了~

据说,控制iis和别的ms的垃圾(ms的ftp, 用户管理.....)可以用adsi和wmi(win2k3的才好),

参考了网友们的资料,有版权问题麻烦email一下

原来是按三层写的代码没有整理, 权当笔记,省点稿纸, 大家看个大概, 详细的msdn都有!

三个文件:

////////filename: hostservice.cs

//////////////////////////////////////////////////////////////////////////////////////////////////

using system;
using system.data;

using wooyea.website.modules.hosts.dataaccess;
using wooyea.website.modules.hosts.configuration;

namespace wooyea.website.modules.hosts.business
{
 /// <summary>
 /// summary description for hostservice.
 /// </summary>
 public class hostservice
 {
  #region private fields

  private modulesettings settings;

  private int  id;
  private string name;
  private string description;
  private decimal price;   // the field in sql server is the type of smallmoney
  private string ip;
  private int  port;
  private string rootpath;
  private int  maxbandwidth;
  private int  maxconnections;
  private int  cpulimit;
  private byte serversize;
  private byte appisolated;
  #endregion

  #region properties
  
  public int id
  {
   get {return id;}
   set {id = value;}
  }

  public string name
  {
   get {return name;}
   set {name = value;}
  }

  public string description
  {
   get {return description;}
   set {description = value;}
  }

  public decimal price
  {
   get {return price;}
   set {price = value;}
  }

  public string ip
  {
   get {return ip;}
   set {ip = value;}
  }

  public int port
  {
   get {return port;}
   set {port = value;}
  }

  public string rootpath
  {
   get {return rootpath;}
   set {rootpath = value;}
  }
  
  public int maxbandwidth
  {
   get {return maxbandwidth;}
   set {maxbandwidth = value;}
  }

  public int cpulimit
  {
   get {return cpulimit;}
   set {cpulimit = value;}
  }

  public byte serversize
  {
   get {return serversize;}
   set {serversize = value;}
  }

  #endregion

  public hostservice()
  {
   configuration.moduleconfig config = new moduleconfig();
   settings = config.getsettings();
  }

  public hostservice(string name, string description, decimal price, string ip, string rootpath) : this()
  {
   this.name   = name;
   this.description = description;
   this.price   = price;
   this.ip    = ip;
   this.rootpath  = rootpath;

  }

  public hostservice(int id) : this()
  {
   this.id = id;
   getdetails();
  }

  public void getdetails()
  {
   dataaccess.hostservices datahostservices = new wooyea.website.modules.hosts.dataaccess.hostservices(settings.connectionstring);

   datarow temprow = datahostservices.getdetails(id);

   this.name  = (string)temprow["hostservicename"];
   this.price  = convert.todecimal(temprow["hostserviceprice"]);
   this.ip   = (string)temprow["hostserviceip"];
   this.port  = (int)temprow["hostserviceport"];
   this.rootpath = (string)temprow["hostservicerootpath"];

  }
  
  public dataset gethostservices()
  {
   dataaccess.hostservices datahostservices= new wooyea.website.modules.hosts.dataaccess.hostservices(settings.connectionstring);

   return datahostservices.gethostservices();
  }

  public int create()
  {
   dataaccess.hostservices datahostservices= new wooyea.website.modules.hosts.dataaccess.hostservices(settings.connectionstring);

   return datahostservices.insert(name, description, price, rootpath, ip);
  }

  public bool update()
  {
   dataaccess.hostservices datahostservices= new wooyea.website.modules.hosts.dataaccess.hostservices(settings.connectionstring);

   return datahostservices.update(id, name, description, price, rootpath, ip);
  }

 }
}
////////////end hostservice.cs/////////////////////////////////////////////////////////////////////

// filename: iismanager.cs

using system;
using system.collections;
using system.text.regularexpressions;
using system.text;
using system.directoryservices;

using wooyea.website.modules.hosts;

namespace wooyea.website.modules.hosts.business
{
 /// <summary>
 /// summary description for class1.
 /// </summary>
 class iismanager
 {

  public iismanager()
  {}

  public directoryentry getdirectoryentry(string entrypath)
  {
   // creater direntry instance depend on local or remote
   directoryentry direntry = new directoryentry(entrypath);

   return direntry;
  }

  public bool createsite(websiteinfo newsiteinfo)
  {


   string entpath = "iis://localhost/w3svc";
   directoryentry rootentry = getdirectoryentry(entpath);

   string newsiteid = getnewsiteid();

   directoryentry newsiteentry = rootentry.children.add(newsiteid, "iiswebserver");
   newsiteentry.commitchanges();

   newsiteentry.properties["serverbindings"].value = newsiteinfo.serverbindings;
   newsiteentry.properties["servercomment"].value = newsiteinfo.comment;
   newsiteentry.commitchanges();

   directoryentry vdirentry = newsiteentry.children.add("root", "iiswebvirtualdir");
   vdirentry.commitchanges();

   vdirentry.properties["path"].value = newsiteinfo.path;        //ph patth in disk
   vdirentry.commitchanges();

   return true;

  }

  /// <summary>
  /// get and return a new website id of specify host
  /// </summary>
  /// <returns>the smallest new website id of the host</returns>
  public string getnewsiteid()
  {
   arraylist idlist = new arraylist();
   string tmpstr;

   string entrypath = "iis://localhost/w3svc";
   directoryentry entry = getdirectoryentry(entrypath);
  
   foreach (directoryentry child in entry.children)
   {
    if (child.schemaclassname == "iiswebserver")
    {
     tmpstr = child.name.tostring();
     idlist.add(convert.toint32(tmpstr));
    }
   }

   idlist.sort();

   int i = 1;
   foreach (int id in idlist)
   {
    if (i == id)
    {
     i++;
    }
   }

   return i.tostring();
  }


 }
}

///////////////////end iismanager/////////////////////////////

//file name:  website.cs

using system;
using system.directoryservices;

using wooyea.website.modules.hosts.configuration;

using wooyea.website.modules.hosts.business;
using wooyea.website.modules.hosts.dataaccess;

namespace wooyea.website.modules.hosts.business
{
 /// <summary>
 /// summary description for website.
 /// </summary>
 public class website
 {
  #region private fields

  private modulesettings settings;

  private websiteinfo siteinfo;

  #endregion
  
  #region properties
 
  #endregion

  website()
  {
   configuration.moduleconfig config = new moduleconfig();
   settings = config.getsettings();
  }

  public website(string newheader, string newcomment, string newip, int newport, string newpath) : this()
  {
   this.siteinfo.header = newheader;
   this.siteinfo.ip = newip;
   this.siteinfo.port = newport;
   this.siteinfo.comment = newcomment;
   this.siteinfo.path = newpath;
  }
  /// <summary>
  /// create a new data record in website table and another one in the cross table
  /// </summary>
  /// <param name="userid">userid of current priciple</param>
  /// <returns></returns>
  public int createsite(int userid)
  {


   iismanager iis = new iismanager();
   iis.createsite(siteinfo);

   dataaccess.websites newsite = new websites(settings.connectionstring);
  
   return newsite.add(siteinfo, userid);

  }

 }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表