|
引用:
using System.DirectoryServices;
获取虚拟目录或网站:
private DirectoryEntry IsExistVDir(string hostName, string vDirName, out int webSiteNum)
{
DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + hostName + "/W3SVC");
webSiteNum = 1;
//定位查找已存在的目录
foreach (DirectoryEntry entry in rootEntry.Children) //网站
{
if (entry.SchemaClassName.ToLower() != "IIsWebServer".ToLower()) continue;
foreach (DirectoryEntry en in entry.Children) //虚拟目录
{
if (en.Name.ToLower() == "root")
foreach (DirectoryEntry subEn in en.Children)
if (subEn.Name == vDirName)
return subEn;
}
//在这一级可以判断网站(关键词可以用 ServerComment),
webSiteNum++;
}
webSiteNum--;
return null;
}
获取已存在的虚拟目录或Web属性, 如: IP,PORT,HEADER
private string getIP_PORT_HEADER(string hostName, string vDirName)
{
DirectoryEntry VDir;
int webSiteNum;
VDir = IsExistVDir(hostName, vDirName, out webSiteNum);
if (VDir == null) //不存在
{
//"不存在的<虚拟目录名称>"
return null;
}
//LocalHost/W3SVC/{webSiteNum}/Root/{vDirName} 规则
//VDir={vDirName}当前网站, {webSiteNum}为当前虚拟目录
//PropertyValueCollection pvc = VDir.Parent.Parent.Properties["ServerBindings"];
DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + hostName + "/W3SVC/" + webSiteNum);
PropertyValueCollection pvc = rootEntry.Properties["ServerBindings"];
return pvc[0].ToString();
}
应用程序扩展映射
/// <summary>
/// Add ISAPI Map
/// </summary>
/// <param name="hostName">host name</param>
/// <param name="vDirName">virtual directory name</param>
/// <param name="isAddOrDel">add is true, del is false</param>
private void CreateMap(string hostName, string vDirName, optMethod opt)
{
//要新增的映射,默认执行应用程序指向aspnet_isapi.dll
string[] exts = new string[] { ".rest", ".nav" };
DirectoryEntry VDir;
int webSiteNum;
VDir = IsExistVDir(hostName, vDirName, out webSiteNum);
if (VDir == null) //不存在
{
MessageBox.Show("不存在的<虚拟目录名称>");
return;
}
ArrayList orglist = new ArrayList(VDir.Properties["ScriptMaps"]);
ArrayList newlist = new ArrayList();
NameValueCollection extMaps = GetExtMaps(exts);
foreach (string s in orglist)
{
string[] tokn = s.Split(',');
bool hasExt = false;
foreach (string extmap in extMaps.AllKeys)
{
if (tokn[0].ToLower() == extmap.ToLower())
{
hasExt = true;
}
}
if (!hasExt)
newlist.Add(s);
}
switch (opt)
{
case optMethod.Add:
foreach (string extmap in extMaps.AllKeys)
{
newlist.Add(extMaps[extmap]);
}
break;
case optMethod.Remove:
break;
case optMethod.Update:
foreach (string extmap in extMaps.AllKeys)
{
newlist.Add(extMaps[extmap]);
}
break;
}
try
{
VDir.Properties["ScriptMaps"].Value = newlist.ToArray();
VDir.CommitChanges();
MessageBox.Show("操作成功");
VDir.Dispose();
}
catch (Exception ex)
{
VDir.Dispose();
MessageBox.Show("操作出错: " + ex.Message);
}
}
private NameValueCollection GetExtMaps(string[] exts)
{
//string rootpath = Environment.GetFolderPath(Environment.SpecialFolder.System);
string sysRootPath = Environment.GetEnvironmentVariable("windir");
string path = sysRootPath + "\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_isapi.dll";
string methods = ""; //"GET,HEAD,POST,DEBUG"
string newmap = "," + path + ",5," + methods;
NameValueCollection extMaps = new NameValueCollection();
foreach (string ext in exts)
{
extMaps.Add(ext, ext + newmap);
}
return extMaps;
}
}
/// <summary>
/// ext操作
/// </summary>
enum optMethod
{
Add,
Remove,
Update
}映射调用
//新增
CreateMap("LocalHost", "VirtualDir", optMethod.Add);
//移除
CreateMap("LocalHost", "VirtualDir", optMethod.Remove);
|
|
所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298
本贴地址:https://www.yunweiku.com/thread-99696-1-1.html
上篇帖子:
ASP 0104 : 80004005解决方法,更改iis上传文件大小限制
下篇帖子:
修复错误1004,10005, IIS站点端口被其他程序使用
|