C# 修改IIS相关设置代码
2010-06-29 14:55
561 查看
/// <summary>
/// 从IIS中查找虚拟目录
/// </summary>
/// <param name="prj">虚拟目录名称</param>
/// <returns></returns>
public static System.DirectoryServices.DirectoryEntry FindVirDir( string VirDirName)
{
System.DirectoryServices.DirectoryEntry rlst = null;
//当项目在IIS上打开时,要把URL转换为物理路径
if (prjname.ToLower().IndexOf("http://") >= 0)
{
System.DirectoryServices.DirectoryEntry rootEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc");
int siteID = 1;
bool isFound = false;
//得到现有的站点标识
foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
{
if (entry.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(entry.Name);
//查找IIS的根目录
System.DirectoryServices.DirectoryEntry root = entry.Children.Find("root", "IIsWebVirtualDir");
if (ID >= siteID)
{
foreach (System.DirectoryServices.DirectoryEntry centry in root.Children)
{
if (centry.SchemaClassName == "IIsWebVirtualDir")
{
if (centry.Properties["AppFriendlyName"].Value.ToString().ToLower() == VirDirName.ToLower())
{
rlst = centry;
isFound = true;
break;
}
}
}
if (isFound)
{
break;
}
}
}
}
}
else
{
rlst = prjname;
}
return rlst;
}
//创建虚拟目录
//新建虚拟目录的DirectoryEntry对象
System.DirectoryServices.DirectoryEntry tbEntry = root.Children.Add("虚拟目录名称", root.SchemaClassName);
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.asp,Default.aspx,index.aspx,login.aspx,login.asp";
tbEntry.Properties["AppFriendlyName"][0] = "虚拟目录名称" ;
tbEntry.Properties["AccessScript"][0] = true; //
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 0;
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.Properties["AspEnableParentPaths"][0]=true; //是否支持父路径
tbEntry.CommitChanges(); //提交
/// 从IIS中查找虚拟目录
/// </summary>
/// <param name="prj">虚拟目录名称</param>
/// <returns></returns>
public static System.DirectoryServices.DirectoryEntry FindVirDir( string VirDirName)
{
System.DirectoryServices.DirectoryEntry rlst = null;
//当项目在IIS上打开时,要把URL转换为物理路径
if (prjname.ToLower().IndexOf("http://") >= 0)
{
System.DirectoryServices.DirectoryEntry rootEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc");
int siteID = 1;
bool isFound = false;
//得到现有的站点标识
foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
{
if (entry.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(entry.Name);
//查找IIS的根目录
System.DirectoryServices.DirectoryEntry root = entry.Children.Find("root", "IIsWebVirtualDir");
if (ID >= siteID)
{
foreach (System.DirectoryServices.DirectoryEntry centry in root.Children)
{
if (centry.SchemaClassName == "IIsWebVirtualDir")
{
if (centry.Properties["AppFriendlyName"].Value.ToString().ToLower() == VirDirName.ToLower())
{
rlst = centry;
isFound = true;
break;
}
}
}
if (isFound)
{
break;
}
}
}
}
}
else
{
rlst = prjname;
}
return rlst;
}
//创建虚拟目录
//新建虚拟目录的DirectoryEntry对象
System.DirectoryServices.DirectoryEntry tbEntry = root.Children.Add("虚拟目录名称", root.SchemaClassName);
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.asp,Default.aspx,index.aspx,login.aspx,login.asp";
tbEntry.Properties["AppFriendlyName"][0] = "虚拟目录名称" ;
tbEntry.Properties["AccessScript"][0] = true; //
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 0;
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.Properties["AspEnableParentPaths"][0]=true; //是否支持父路径
tbEntry.CommitChanges(); //提交
相关文章推荐
- c# winform 用代码修改DataGridView列头的名字,设置列名,修改列名
- c#读取IIS中的站点相关属性代码
- c#读取IIS中的站点相关属性代码
- c# winform 用代码修改DataGridView列头的名字,设置列名,修改列名
- 防止对 Visual Basic .NET 或 C# 代码进行反相工程 已经相关原理介绍
- c#建立快捷方式代码并且设置为管理员权限
- WPF Grid 用 C# 代码后台设置
- c#环境XML文件创建、修改、删除、查找相关操作
- SYD8801代码解析二【复位等特殊系统行为要等待硬件响应】【协议栈上报事件处理】【时钟源准确度的优化】【修改蓝牙名称】【设置中断优先级】
- 用C#操纵IIS(代码)
- [导入]用C#操纵IIS(代码)
- C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
- 一段C#代码,模拟IIS功能
- 用asp实现的代码批量修改程序,fso相关
- IIS权限设置的安全相关设置
- C#中,设置字体,自动换行,保存文件对话框代码
- C# WinForm (笨方法)根据不同的样式配置 设置窗体相关控件的背景 以改变窗体风格
- 【代码】Nginx 防跨目录设置方法之修改PHP源代码
- C# CLRInsideOut 托管代码与非托管代码互操作,产生相关调用代码的好工具 C++ 头文件转C# 的好工具
- Word转换成pdf在vs调试可以成功;发布到IIS上失败的相关设置