亲宝软件园·资讯

展开

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

软件工程师 人气:0
想了解C#实现创建,删除,查找,配置虚拟目录实例详解的相关内容吗,软件工程师在本文为您仔细讲解C#创建,删除,查找,配置虚拟目录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,创建,删除,查找,配置,虚拟目录,下面大家一起来学习吧。

本文实例讲述了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#程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论