C#实现创建,删除,查找,配置虚拟目录实例详解
作者:软件工程师 时间:2022-09-27 06:38:16
本文实例讲述了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#程序设计有所帮助。
标签:C#,虚拟目录
0
投稿
猜你喜欢
Java中线程的等待与唤醒_动力节点Java学院整理
2023-07-17 04:03:05
Python安装Jupyter Notebook配置使用教程详解
2023-08-31 02:52:03
C#实现的JS操作类实例
2023-11-13 05:19:48
Java中volatile关键字的作用
2023-06-02 17:02:55
Java中的"goto"语句妙用
2023-10-10 03:54:32
Spring类型转换 ConversionSerivce Convertor解析
2022-03-25 06:55:46
一文搞懂Spring中的注解与反射
2023-11-11 11:38:18
一文总结Java获取文件后缀名的所有方法
2022-10-18 16:06:40
Eclipse新建Android项目报错解决方案详细汇总
2022-12-12 10:53:56
详解C#中Helper类的使用
2021-10-05 07:37:28
Android实现未读消息小红点显示实例
2022-05-18 07:57:27
Flutter 设置全局字体的实现
2023-05-04 06:27:25
深入了解Java ServletContext
2023-11-08 22:36:27
Java双向链表的操作
2021-07-31 02:54:33
基于springboot的flowable工作流实战流程分析
2022-11-28 08:27:09
C#通过属性名字符串获取、设置对象属性值操作示例
2022-01-06 14:28:08
C# listview 点击列头排序的实例
2023-11-26 20:09:05
WinForm下 TextBox只允许输入数字的小例子
2023-08-02 15:34:14
Android通过Handler与AsyncTask两种方式动态更新ListView(附源码)
2022-04-14 03:32:22
Android沉浸式顶部实现代码及效果
2021-06-19 18:51:49