论坛: 网站建设 标题: IISAdministrationusingADSIand.NETDirectoryServicesNamespace 复制本贴地址    
作者: billchen [billchen]    论坛用户   登录

Getting Virtual Directories 


public StringCollection GetIISVirtualDirectories(string provider)
{
     provider = "IIS://192.168.1.150/W3SVC/1/Root";
     DirectoryEntries dirEntries = null;
     try
     {
        dirEntry = new DirectoryEntry(provider); 
        dirEntry.RefreshCache();
        dirEntries = dirEntry.Children;
        strCollection = new StringCollection();
        foreach(DirectoryEntry objDir in dirEntries)
        {
           strCollection.Add(objDir.Name);
         }
        dirEntry.Dispose();
     }
     catch(Exception ex)
     {
       System.Windows.Forms.MessageBox.Show("Error getting virtual director: " + ex);
       return null;
     }
     return strCollection;
}

 









Creating Virtual Directories 



public int createVirtualDirectory(string ipaddress,string strRootPath, string schema , string sitename , string path)
{
    int success = 0;
    DirectoryEntry dirRoot = new DirectoryEntry("IIS://" + ipaddress + strRootPath.Trim());
    //Check whether the directory already exists on the server
    if(DirectoryEntry.Exists("IIS://" + ipaddress + strRootPath + "/" +sitename))
    return 0;//Return 0 to display error message
    try
    {
        //If not exists, add a new virtual directoty 
        dirRoot.RefreshCache();
        DirectoryEntry dirNew = dirRoot.Children.Add    (sitename,schema);
        dirNew.Properties["Path"].Insert(0,path);
        dirNew.CommitChanges();
        dirRoot.CommitChanges();
        //If it is web virtaul directory
       if(schema == "IIsWebVirtualDir")
      {
          //dirNew.Invoke("AppCreate",true);
          dirNew.Invoke("AppCreate",new object[]{true});
          dirNew.CommitChanges();
          dirRoot.CommitChanges();
          dirNew.Close();
          dirRoot.Close();
        }
        success = 1;
    }
    catch
    {
        throw new IISPropertiesException("Unable to create the virtual directory"); 
     }
    return success;
}
 
 
public class IISPropertiesException : Exception
{
     /// <summary>
    /// Constructs a new IISPropertiesException class
    /// </summary>
    /// <param name="description">Speicifes the error description</param>
   public IISPropertiesException(string description): base(description){}
   public override string ToString()
   {
      return "IISProperties Exception : " + Message + " " + StackTrace;
    }
}

 


地主 发表时间: 06/22 09:45

论坛: 网站建设

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号