C#創立IIS虛擬目次的辦法。本站提示廣大學習愛好者:(C#創立IIS虛擬目次的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#創立IIS虛擬目次的辦法正文
本文實例講述了C#創立IIS虛擬目次的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
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();
System.DirectoryServices.DirectoryEntries
IIs創立虛擬目次
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace Install_IIS
{
class IISManager
{
public IISManager()
{
}
/// <summary>
/// 創立虛擬目次
/// </summary>
/// <param name="WebSite">辦事器站點稱號</param>
/// <param name="VDirName">虛擬目次稱號</param>
/// <param name="Path"></param>
/// <param name="RootDir"></param>
/// <param name="chkRead"></param>
/// <param name="chkWrite"></param>
/// <param name="chkExecute"></param>
/// <param name="chkScript"></param>
/// <param name="chkAuth"></param>
/// <param name="webSiteNum">1</param>
/// <param name="serverName">localhost</param>
/// <returns></returns>
public string CreateVDir(string WebSite,string VDirName, string Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName)
{
string sRet=String.Empty;
System.DirectoryServices.DirectoryEntry IISSchema;
System.DirectoryServices.DirectoryEntry IISAdmin;
System.DirectoryServices.DirectoryEntry VDir;
bool IISUnderNT;
//
// 肯定IIS版本
//
IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN")
IISUnderNT=true;
else
IISUnderNT=false;
IISSchema.Dispose();
//
// Get the admin object
// 取得治理權限
//
IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");
if (IISAdmin == null)
return "IIS 未正常裝置";
if (IISAdmin.Children == null)
return "IIS 能夠未啟動";
//
// If we're not creating a root directory
// 假如我們不克不及創立一個根目次
//
if (!RootDir)
{
//
// If the virtual directory already exists then delete it
// 假如虛擬目次曾經存在則刪除
//
foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
{
if (v.Name == VDirName)
{
// Delete the specified virtual directory if it already exists
try
{
IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName });
IISAdmin.CommitChanges();
}
catch(Exception ex)
{
sRet+=ex.Message;
}
}
}
}
//
// Create the virtual directory
// 創立一個虛擬目次
//
if (!RootDir)
{
VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
}
else
{
VDir = IISAdmin;
}
//
// Make it a web application
// 創立一個web運用
//
VDir.Properties["Path"][0] = Path;
//設置虛擬目次指向的物理途徑
if (IISUnderNT)
{
VDir.Invoke("AppCreate", false);
}
else
{
VDir.Invoke("AppCreate", 1);
}
//
// Setup the VDir
// 設置虛擬目次
//
VDir.Properties["AccessRead"][0] = chkRead; //設置讀取權限
VDir.Properties["AccessExecute"][0] = chkExecute; //設置履行權限
VDir.Properties["AccessWrite"][0] = chkWrite; //設置寫入權限
VDir.Properties["AccessScript"][0] = chkScript; //履行權限
VDir.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";//設置默許文檔,多值情形下中央用逗號朋分
VDir.Properties["AppFriendlyName"][0] = VDirName; //運用法式稱號
VDir.Properties["AuthFlags"][0] = 0; // 設置目次的平安性,0表現不許可匿名拜訪,1為許可,3為根本身份驗證,7為windows繼續身份驗證
VDir.Properties["AuthNTLM"][0] = chkAuth;
VDir.Properties["EnableDefaultDoc"][0] = true;
VDir.Properties["EnableDirBrowsing"][0] = false;
//
// NT doesn't support this property
// NT格局不支撐這特征
//
if (!IISUnderNT)
{
VDir.Properties["AspEnableParentPaths"][0] = true;
}
//
// Set the changes
// 設置轉變
//
VDir.CommitChanges();
//上面的辦法是獲得一切屬性稱號的辦法:
foreach (PropertyValueCollection pvc in VDir.Properties)
{
Console.WriteLine(pvc.PropertyName);
}
sRet+= "VRoot " +VDirName + " created!";
return sRet;
}
#region Properties
public string ServerName
{
get
{
return _serverName;
}
set
{
_serverName = value;
}
}
#endregion
public static string VirDirSchemaName = "IIsWebVirtualDir";
#region Private Members
private string _serverName;
#endregion
}
}
測試用:
MessageBox.Show(new IISManager().CreateVDir("localhost", "ietm", "c:\\myweb", false, true, false, false, true, false, 1, "localhost"));
這個我已投入項目中應用,可寧神應用。
願望本文所述對年夜家的C#法式設計有所贊助。