使用c#建立虚拟目录
2007-01-10 12:03
295 查看
使用c#建立虚拟目录
分类:C#&ASP.NET代码const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
string virtualDirName = "virtualName";//虚拟目录名称
string physicalPath = @"c:/1";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);
tbEntry.Properties["Path"][0] = physicalPath;
tbEntry.Invoke("AppCreate", true);
tbEntry.Properties["AccessRead"][0] = false;
tbEntry.Properties["ContentIndexed"][0] = true; ;
tbEntry.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
tbEntry.Properties["AccessScript"][0] = true;
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 0;
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
现在来解释一下这段代码:
创建虚拟目录需要使用System.DirectoryEntries命名空间下的DirectoryEntry类
首先声明一个DirecotryEntry的实例
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
其中的参数constIISWebSiteRoot由一下几个部分组成
"IIS://"+domainName+"/W3SVC"+WebSiteNumber+"/ROOT"
WebSiteNumber由你要在哪个站点创建虚拟目录来决定,xp只能有一个站点,所以WebSiteNumber值为1,win2000Server 和win2003可以创建多个站点,所以WebSiteNumber的值可以大于1
然后创建DirecotryEntry实例的子节点 ,也就是要创建的虚拟目录
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);
root.Children.Add(string namespace,string schemaClassName)方法会创建并返回一个DirectoryEntry的实例
下面的部分就是对这个新创建的节点进行操作了
tbEntry.Properties["Path"][0] = physicalPath; //设置虚拟目录指向的物理路径
tbEntry.Invoke("AppCreate", true); //
tbEntry.Properties["AccessRead"][0] = false; //设置读取权限
tbEntry.Properties["DefaultDoc"][0] = "index.asp,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; //应用程序名称
tbEntry.Properties["AccessScript"][0] = true; //执行权限
tbEntry.Properties["AuthFlags"][0] = 0; // 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.CommitChanges();//
这里这是列举了部分属性,如果得到更多属性的信息可是访问DirecotryEntry.Properties属性
下面的方法是得到所有属性名称的方法:
foreach (PropertyValueCollection pvc in tbEntry.Properties)
{
Console.WriteLine(pvc.PropertyName);
}
如若建立不了,请看下面
参照站点:http://www.microsoft.com/china/technet/community/columns/insider/iisi0205.mspx站点
相关文章推荐
- 使用c#建立虚拟目录
- 使用c#建立虚拟目录
- C#建立IIS虚拟目录站点
- C#本地iis建立虚拟目录
- 使用虚拟目录建立DedeCMS站点应注意的地方
- 使用C++操作IIS的虚拟目录
- 使用ICE建立C++与C#的通讯
- 使用C++操作IIS的虚拟目录
- IIS的部署(二)------虚拟目录的使用
- 使用mkdir建立多个目录
- 初探ASP.NET之虚拟目录的建立!
- tomcat 建立虚拟目录
- C# 获取IIS站点及虚拟目录信息
- Apache使用一个IP地址建立多个虚拟主机
- C# 创建虚拟目录
- strchr()库函数使用(顺次建立多层目录)
- Ubuntu Apcahe服务虚拟目录的配置 phpmyadmin的架设使用
- centos7 建立虚拟目录
- 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。
- C#虚拟(virtual)自动属性使用时的一点注意事项