Home>

c#operation iis method collection

If the error is reported in win8 and win7:unknown error (0x80005000) ---- see/go.php?id=72881&s=a

using system;
using system.collections;
using system.collections.generic;
using system.directoryservices;
using system.linq;
using system.net;
using system.text;
using system.threading.tasks;
namespace iiscontrolhelper
{
  ///<summary>
  ///iis operation method collection
  ////go.php?id=72881&s=a error
  ///</summary>
  public class iisworker
  {
    private static string hostname="localhost";
    ///<summary>
    ///get the local iis version
    ///</summary>
    ///<returns></returns>
    public static string getiisversion ()
    {
      try
      {
        directoryentry entry=new directoryentry ("iis://" + hostname + "/w3svc/info");
        string version=entry.properties ["majoriisversionnumber"]. value.tostring ();
        return version;
      }
      catch (exception se)
      {
        //Explanation:there is no (int) entry.properties ["majoriisversionnumber"]. Value;property in iis5.0, an exception will be thrown and the version is 5.0
        return string.empty;
      }
    }
    ///<summary>
    ///Create a virtual directory website
    ///</summary>
    ///<param name="websitename">site name</param>
    ///<param name="physicalpath">physical path</param>
    ///<param name="domainport">site + port, such as 192.168.1.23:90</param>
    ///<param name="iscreateapppool">Whether to create a new application pool</param>
    ///<returns></returns>
    public static int createwebsite (string websitename, string physicalpath, string domainport, bool iscreateapppool)
    {
      directoryentry root=new directoryentry ("iis://" + hostname + "/w3svc");
      //find an unused id for the 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", domainport + ":");
      site.invoke ("put", "serverstate", 2);
      site.invoke ("put", "frontpageweb", 1);
      site.invoke ("put", "defaultdoc", "default.html");
      //site.invoke ("put", "securebindings", ":443:");
      site.invoke ("put", "serverautostart", 1);
      site.invoke ("put", "serversize", 1);
      site.invoke ("setinfo");
      //Create the application virtual directory
      directoryentry sitevdir=site.children.add ("root", "iiswebvirtualdir");
      sitevdir.properties ["appisolated"] [0]=2;
      sitevdir.properties ["path"] [0]=physicalpath;
      sitevdir.properties ["accessflags"] [0]=513;
      sitevdir.properties ["frontpageweb"] [0]=1;
      sitevdir.properties ["approot"] [0]="lm/w3svc /" + siteid + "/root";
      sitevdir.properties ["appfriendlyname"] [0]="root";
      if (iscreateapppool)
      {
        directoryentry apppools=new directoryentry ("iis://" + hostname + "/w3svc/apppools");
        directoryentry newpool=apppools.children.add (websitename, "iisapplicationpool");
        newpool.properties ["apppoolidentitytype"] [0]="4";//4
        newpool.properties ["managedpipelinemode"] [0]="0";//0:integrated mode 1:classic mode
        newpool.commitchanges ();
        sitevdir.properties ["apppoolid"] [0]=websitename;
      }
      sitevdir.commitchanges ();
      site.commitchanges ();
      return siteid;
    }
    ///<summary>
    ///Get the physical path of the website
    ///</summary>
    ///<param name="rootentry">site node</param>
    ///<returns></returns>
    public static string getwebsitephysicalpath (directoryentry rootentry)
    {
      string physicalpath="";
      foreach (directoryentry childentry in rootentry.children)
      {
        if ((childentry.schemaclassname == "iiswebvirtualdir")&&(childentry.name.tolower () == "root"))
        {
          if (childentry.properties ["path"]. value!=null)
          {
            physicalpath=childentry.properties ["path"]. value.tostring ();
          }
          else
          {
            physicalpath="";
          }
        }
      }
      return physicalpath;
    }
    ///<summary>
    ///get site name
    ///</summary>
    public static list<iisinfo>getserverbindings ()
    {
      list<iisinfo>iislist=new list<iisinfo>();
      string entpath=string.format ("iis://{0}/w3svc", hostname);
      directoryentry ent=new directoryentry (entpath);
      foreach (directoryentry child in ent.children)
      {
        if (child.schemaclassname.equals ("iiswebserver", stringcomparison.ordinalignorecase))
        {
          if (child.properties ["serverbindings"]. value!=null)
          {
            object objectarr=child.properties ["serverbindings"]. value;
            string serverbindingstr=string.empty;
            if (isarray (objectarr)) //If there are multiple binding sites
            {
              object [] objecttoarr=(object []) objectarr;
              serverbindingstr=objecttoarr [0] .tostring ();
            }
            else //only one binding site
            {
              serverbindingstr=child.properties ["serverbindings"]. value.tostring ();
            }
            iisinfo iisinfo=new iisinfo ();
            iisinfo.domainport=serverbindingstr;
            iisinfo.apppool=child.properties ["apppoolid"]. value.tostring ();//application pool
            iislist.add (iisinfo);
          }
        }
      }
      return iislist;
    }
    public static bool createapppool (string apppoolname, string username, string password)
    {
      bool issucess=false;
      try
      {
        //Create a new program pool
        directoryentry newpool;
        directoryentry apppools=new directoryentry ("iis://" + hostname + "/w3svc/apppools");
        newpool=apppools.children.add (apppoolname, "iisapplicationpool");
        //Set properties Access username and password Generally adopt the default method
        newpool.properties ["wamusername"] [0]=username;
        newpool.properties ["wamuserpass"] [0]=password;
        newpool.properties ["apppoolidentitytype"] [0]="3";
        newpool.commitchanges ();
        issucess=true;
        return issucess;
      }
      catch //(exception ex)
      {
        return false;
      }
    }
    ///<summary>
    ///Associate the corresponding application and virtual directory after the program pool is established
    ///</summary>
    public static void setapptopool (string appname, string poolname)
    {
      //Get the directory
      directoryentry getdir=new directoryentry ("iis://localhost/w3svc");
      foreach (directoryentry getentity in getdir.children)
      {
        if (getentity.schemaclassname.equals ("iiswebserver"))
        {
          //Set the application program pool Get the application first Set the application program pool
          //Test the root directory for the first time
          foreach (directoryentry getchild in getentity.children)
          {
            if (getchild.schemaclassname.equals ("iiswebvirtualdir"))
            {
              //Find the specified virtual directory.
              foreach (directoryentry getsite in getchild.children)
              {
                if (getsite.name.equals (appname))
                {
                  //[Test passed successfully]
                  getsite.properties ["apppoolid"]. value=poolname;
                  getsite.commitchanges ();
                }
              }
            }
          }
        }
      }
    }
    ///<summary>
    ///determine if the object is an array
    ///</summary>
    public static bool isarray (object o)
    {
      return o is array;
    }
  }
}
c
  • Previous Android desktop component App Widget complete case
  • Next Parsing uncaught exceptions in Java and nested use of try statements