欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现创建,删除,查找,配置虚拟目录实例详解

程序员文章站 2023-11-16 22:23:46
本文实例讲述了c#实现创建,删除,查找,配置虚拟目录的方法。分享给大家供大家参考。具体如下: #region<<虚拟目录>> /// &...

本文实例讲述了c#实现创建,删除,查找,配置虚拟目录的方法。分享给大家供大家参考。具体如下:

#region<<虚拟目录>>
/// <summary>
/// 创建虚拟目录
/// </summary>
/// <param >虚拟目录别名</param>
/// <param >内容所在路径</param>
public static bool createvirtualdirectory(string website,string virtualname, string path)
{
 if (virtualname.equals(string.empty) || (!directory.exists(path)))
 {
  messagebox.show("1.虚拟目录别名不能为空/r/n 2.虚拟目录内容所在路径可能不存在", "提示", messageboxbuttons.ok, messageboxicon.warning);
  return false;
 }
 try
 {
  directoryentry tbentry = findvirtualdir(website,virtualname);//查找虚拟目录
  if (tbentry == null) //找不到则创建虚拟目录
  {
   string iispath = "iis://localhost/w3svc/"+website+"/root";//iis参数格式
   system.directoryservices.directoryentry root = new directoryentry(iispath);
   tbentry = root.children.add(virtualname, root.schemaclassname);//创建虚拟目录
  }
  return updatevirdir(tbentry, path, virtualname);
 }
 catch (exception ex)
 {
  messagebox.show("1.请确认iis是否已经安装/r/n2.虚拟目录有可能已经存在/r/n (error:" + ex.tostring(), "提示", messageboxbuttons.ok, messageboxicon.warning);
  writerlog("createvir:/r/n" + ex.tostring());
  return false;
 }
}
/// <summary>
/// 获取网站列表
/// </summary>
/// <returns></returns>
public static list<websiteinfo> getwebsitelist()
{
 try
 {
  list<websiteinfo> infolist = new list<websiteinfo>();
  directoryentry deroot = new directoryentry("iis://localhost/w3svc");
  foreach (directoryentry e in deroot.children)
  {
   websiteinfo info = new websiteinfo();
   propertyvaluecollection pvc = e.properties["serverbindings"];
   if (pvc.count <= 0)
    continue;
   string[] srvbindings = pvc[0].tostring().split(':');
   info.ipaddress = srvbindings[0] == string.empty ? appcode.toolbox.getip() : srvbindings[0];//绑定的ip地址
   info.port = srvbindings[1];//对应端口号
   info.name = e.name;//所在站点索引
   infolist.add(info);
  }
  return infolist;
 }
 catch (exception ex)
 {
  writerlog(ex.tostring());
  return null;
 }
}
/// <summary>
/// 删除虚拟目录
/// </summary>
/// <param ></param>
/// <param ></param>
/// <returns></returns>
public static bool delvirtualdirectory(string website,string namedirectory)
{
 try
 {
  directoryentry deroot = new directoryentry("iis://localhost/w3svc/" + website + "/root");
  try
  {
   deroot.children.remove(findvirtualdir(website,namedirectory));
   deroot = null;
   return true;
  }
  catch
  {
   return true;
  }
 }
 catch (exception e)
 {
  writerlog(e.tostring());
  return false;
 }
}
/// <summary>
/// 修改虚拟目录
/// </summary>
/// <param ></param>
/// <param ></param>
/// <param ></param>
/// <returns></returns>
public static bool updatevirdir(directoryentry tbentry, string path, string virtualname)
{
 if (tbentry == null)
  return false;
 tbentry.properties["path"][0] = path;//物理目录
 tbentry.invoke("appcreate", true);//创建应用程序
 tbentry.properties["accessread"][0] = true;//允许访问
 tbentry.properties["contentindexed"][0] = true; ;//
 tbentry.properties["defaultdoc"][0] = "index.asp,default.aspx";
 tbentry.properties["appfriendlyname"][0] = virtualname;//应用程序名
 tbentry.properties["accessscript"][0] = true;//访问权限
 tbentry.properties["dontlog"][0] = true;//日志
 tbentry.properties["authflags"][0] = 1;//验证,0不,1允许,3基本身份
 tbentry.commitchanges();//开始应用属性
 return true;
}
/// <summary>
/// 查找虚拟目录
/// </summary>
/// <param ></param>
/// <returns></returns>
public static directoryentry findvirtualdir(string website,string virtualname)
{
 if (virtualname.equals(string.empty))
 {
  messagebox.show("虚拟目录别名不能为空", "提示", messageboxbuttons.ok, messageboxicon.warning);
  return null;
 }
 try
 {
  string iispath = "iis://localhost/w3svc/" + website + "/root";//iis参数格式
  system.directoryservices.directoryentry root = new directoryentry(iispath);//实例iis类
  return root.children.find(virtualname, root.schemaclassname);//查找虚拟目录)
 }
 catch (exception ex)
 {
  writerlog("虚拟目录不存在: " + ex.message);
  return null;
 }
}
/// <summary>
/// 为虚拟目录增加扩展名
/// </summary>
/// <param >虚拟目录名</param>
/// <param >扩展名列表</param>
public static void setmime(string website,string virtualname, dictionary<string, string> exlist)
{
 if (virtualname.equals(string.empty))
 {
  messagebox.show("虚拟目录别名不能为空", "提示", messageboxbuttons.ok, messageboxicon.warning);
  return;
 }
 directoryentry tbentry = findvirtualdir(website, virtualname);
 if (tbentry == null)
  return;
 system.directoryservices.propertyvaluecollection mime = tbentry.properties["mimemap"];
 foreach (string e in exlist.keys)
 {
  iisole.mimemapclass ex = new iisole.mimemapclass();
  ex.extension = e;
  ex.mimetype = exlist[e];
  if (contianvalue(ex, mime) != null)
   continue;
  mime.add(ex);
  ex = null;
 }
 tbentry.commitchanges();
}
/// <summary>
/// 删除扩展名
/// </summary>
/// <param ></param>
/// <param ></param>
/// <returns></returns>
public static bool delmime(iisole.iismimetype type,string website, string virtualname)
{
 directoryentry tbentry = findvirtualdir(website, virtualname);
 if (tbentry == null)
  return false;
 system.directoryservices.propertyvaluecollection mime = tbentry.properties["mimemap"];
 object v = contianvalue(type, mime);
 if (v != null)
  mime.remove(v);
 tbentry.commitchanges();
 return true;
}
/// <summary>
/// 搜索取指定扩展名对象
/// </summary>
/// <param ></param>
/// <param ></param>
/// <returns></returns>
private static object contianvalue(iisole.iismimetype type, propertyvaluecollection mime)
{
 foreach (object v in mime)
 {
  iisole.iismimetype e = (iisole.iismimetype)v;
  if (type.extension.tolower().equals(e.extension.tolower()) && (type.mimetype.tolower().equals(e.mimetype.tolower())))
  {
   e = null;
   return v;
  }
 }
 return null;
}
/// <summary>
/// 获得虚拟目录的扩展名列表
/// </summary>
/// <param ></param>
/// <returns></returns>
public static dictionary<string, string> getmimelist(string website,string virtualname)
{
 if (virtualname.equals(string.empty))
 {
  messagebox.show("虚拟目录别名不能为空", "提示", messageboxbuttons.ok, messageboxicon.warning);
  return null;
 }
 try
 {
  dictionary<string, string> res = new dictionary<string, string>();
  directoryentry tbentry = findvirtualdir(website,virtualname);
  if (tbentry == null)
   return null;
  system.directoryservices.propertyvaluecollection _mime = tbentry.properties["mimemap"];
  foreach (object e in _mime)
  {
   iisole.iismimetype v = (iisole.iismimetype)e;
   if (!res.containskey(v.extension))
    res.add(v.extension, v.mimetype);
   v = null;
  }
  return res;
 }
 catch (exception ex)
 {
  writerlog("getmimelist; " + ex.message);
  return null;
 }
}
#endregion

希望本文所述对大家的c#程序设计有所帮助。