一、查看虚拟目录是否存在
private
bool IsExitesVirtualDir(string virtualdirname)
{
bool exited =false;
DirectoryEntry _entry = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
DirectoryEntries _entries = _entry.Children;
foreach(DirectoryEntry _cen in _entries)
{
if(_cen.Name
== virtualdirname)
exited = true;
}
return exited;
}
其中virtualdirpath指要建立的虚拟目录名称;
二、新增虚拟目录
private
void CreateVirtualDir(string virtualdirname,string logicDir)
{
if(IsExitesVirtualDir(virtualdirname))
DeleteVirtualDir(virtualdirname);
DirectoryEntry _rootEntry ;
_rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
DirectoryEntry _newVirDir;
_newVirDir = _rootEntry .Children.Add(virtualdirpath,"IIsWebVirtualDir");
_newVirDir.Invoke("AppCreate",true);
_newVirDir.CommitChanges();
_rootEntry .CommitChanges();
_newVirDir.Properties["AnonymousPasswordSync"][0] = true;
_newVirDir.Properties["Path"][0] = logicDir +
@"virtualdirentry\virtualname\";
_newVirDir.CommitChanges();
}
_newVirDir.Properties["Path"][0]
的值为虚拟目录对应的物理地址;
三、删除虚拟目录
private
void DeleteVirtualDir(string virtualdirname)
{
DirectoryEntry _rootEntry ;
_rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
object[] paras = new object[2];
paras[0] = "IIsVirtualDir";
paras[1] = virtualdirname;
_rootEntry .Invoke("Delete",paras);
_rootEntry .CommitChanges();
}
如果是在B/S模式下的应用可能会遇到操作权限的问题,可根据不同的系统尝试一下方法!
1,打开web.config,然后在system.web节点中添加一属性
<identity impersonate="true" />
2,目录的EveryOne权限设为全部。
3,把aspnet设置成
system 权限
.net C# 对虚拟目录IIS的操作,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/yplong/p/3731104.html