DirectionEntry 常用操作
前些天修改一个安装程序,需要用到操作IIS 的site,可以使用c#提供的DirectoryEntry类来操作。
MSDN上有DirectoryEntry的说明,但没有找到提供的例子。我在网上找了下,在这里列出我用的几个常用的操作。
public static int CreateWebSite(string webSiteName, string pathToRoot,string serverip,int port,string defPage, bool createDir)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
int siteID = 1;
bool hasWebSite = false;
//判断website是否已经存在
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(e.Name);
string webName = e.Properties["ServerComment"].Value.ToString();
if (webName.ToUpper() == webSiteName.ToUpper())
{
hasWebSite = true;
siteID = ID ;
break;
}
else if(ID >= siteID)
{
siteID = ID + 1;
}
}
}
// Create web site
DirectoryEntry site= null;
if (!hasWebSite)
site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
else
site = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID.ToString());
//使用put方法设置
site.Invoke("Put", "ServerComment", webSiteName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", serverip + ":" + port.ToString() + ":");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", defPage);
site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
// Create application virtual directory
DirectoryEntry siteVDir = null;
if (!hasWebSite)
siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
else
siteVDir = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID.ToString() + "/root");
siteVDir.Properties["Path"][0] = pathToRoot;
siteVDir.Properties["AppFriendlyName"][0] = "Test";
siteVDir.Invoke("AppCreate", true);
//有些属性是默认的,不必要设置
siteVDir.Properties["AuthAnonymous"][0] = true;
siteVDir.Properties["AccessRead"][0] = true;
siteVDir.Properties["ContentIndexed"][0] = false;
siteVDir.Properties["DefaultDoc"][0] = "Default.aspx";
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["AccessScript"][0] = true;
siteVDir.Properties["DontLog"][0] = true;
siteVDir.CommitChanges();
site.CommitChanges();
return siteID;
}
public static bool CreateHttpVirtualDir(int siteIndex, string virtualDirName, string virtualDirPath)
{
//参数验证略下
if (!virtualDirPath.EndsWith(""""))
virtualDirPath += """";
string constIISWebSiteRoot = "IIS://localhost/W3SVC/" + siteIndex.ToString() + "/ROOT";//特别注意root后面带不带斜杠的却别
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry entry = new DirectoryEntry(constIISWebSiteRoot + "/" + virtualDirName);
DirectoryEntry tbEntry = null;
try
{
tbEntry = root.Children.Find(virtualDirName, "IIsWebVirtualDir");
}
catch
{
try
{
tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
}
catch
{
}
}
if (tbEntry != null)
{
tbEntry.Properties["Path"][0] = virtualDirPath;
tbEntry.Invoke("AppCreate", true);//对应 IIS设置里的应用程序
tbEntry.Properties["AuthAnonymous"][0] = true;
tbEntry.Properties["AccessRead"][0] = true;
tbEntry.Properties["ContentIndexed"][0] = false;
tbEntry.Properties["DefaultDoc"][0] = "Default.aspx";
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
tbEntry.Properties["AppIsolated"][0] = 2;
tbEntry.Properties["AccessScript"][0] = true;
tbEntry.Properties["DontLog"][0] = true;
tbEntry.CommitChanges();
return true;
}
return false;
}
public static void DelVietualPath(string sitename, int topSiteId)
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/" + topSiteId.ToString() + "/ROOT");
DirectoryEntry entry = new DirectoryEntry();
if (root != null)
{
try
{
object[] paras = new object[2];
paras[0] = "IIsWebVirtualDir";
paras[1] = sitename;
root.Invoke("Delete", paras);
// 使用子节点的操作会报错,所以使用root的delete
//entry = root.Children.Find(sitename, "IIsWebVirtualDir");
//entry.Invoke("AppDelete", true);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
root.CommitChanges();//不commit也能保存,奇怪,不知道原因
}
}
public static void DelWebsite(int siteid) //这个就简单了
{
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
if (siteid > 0)
{
root.Invoke("Delete", "IIsWebServer", siteid);
root.CommitChanges();
}
}
见有人把DirectoryEntry的操作和一些属性封装成一个Manage类,其实,如果你对iis 的操作较多或有更深层的操作,建议你这么做,如果就是使用那么一两下的话,写个类,里面用static 的方法也就够用了吧。
DirectoryEntry也可以操作其他的东西比如常用的ftp类的。跟他功能相当的还有iis 本身的命令操作,如:(iisweb|iisvdir )(/create |/delete /stop),iisftp,iisftpdr 等命令。