使用背景
虚拟目录(virtual directory),计算机术语,每个 Internet服务可以从多个目录中发布。通过以通用命名约定 (UNC) 名、用户名及用于访问权限的密码指定目录,可将每个目录定位在本地驱动器或网络上。指定客户 URL地址,
服务将整个发布目录集提交给客户作为一个目录树。宿主目录是“虚拟”目录树的根。虚拟目录的实际子目录对于客户也是可用的。只有http://www.服务支持虚拟服务器;而 FTP和 gopher服务则只能有一个宿主目录。
虚拟目录不出现于目录列表中(也称为http://www.服务的“目录浏览”)。要访问虚拟目录,用户必须知道虚拟目录的别名,并在浏览器中键入 URL即可访问。
简单来说:一般的情况下,我们的文件保存路径为默认根目录。但是,需要进行访问其他磁盘中的数据时就需要进行创建虚拟目录来进行访问了。
那么具体怎么创建并且使用呢?话不多说,开撸!
配置
1,引用using System.DirectoryServices;
2,在web.config文件的 <system.web>下增加节点:
<identity impersonate="true" userName="Administrator" password="" />
其中userName为计算机用户名称,password为计算机登录密码没有则填写空。需要有这个标识用来操作iis的虚拟目录。
创建
新增一个帮助类,然后写入以下代码(备注写的都很清楚):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/// <summary> /// 删除虚拟路径 /// </summary> /// <param name="virtualdirname"></param> public static void DeleteVirtualDir( string virtualdirname) { DirectoryEntry _rootEntry; _rootEntry = new DirectoryEntry( "IIS://localhost/W3SVC/" + SiteId + "/root" ); object [] paras = new object [2]; paras[0] = "IIsVirtualDir" ; paras[1] = virtualdirname; _rootEntry.Invoke( "Delete" , paras); _rootEntry.CommitChanges(); } /// <summary> /// 创建虚拟目录 /// </summary> /// <param name="siteId">iis站点ID</param> /// <param name="dirName">虚拟文件夹名称</param> /// <param name="path">物理路径</param> /// <param name="userName">用户名称</param> /// <param name="userPass">用户密码</param> /// <param name="appPoolId">应用池Id</param> /// <returns></returns> public static string CreateVirtualDir( string siteId, string dirName, string path, string userName, string userPass, string appPoolId) { string constIISWebSiteRoot = "IIS://localhost/W3SVC/" + siteId + "/ROOT" ; string virtualDirName = dirName; //虚拟目录名称 string physicalPath = path; try { DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot); foreach (System.DirectoryServices.DirectoryEntry v in root.Children) { if (v.Name == dirName) { DeleteVirtualDir(dirName); } } DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir" ); tbEntry.Invoke( "AppCreate" , true ); //设置虚拟目录指向的物理路径 tbEntry.Properties[ "Path" ][0] = physicalPath; //设置读取权限 tbEntry.Properties[ "AccessRead" ][0] = true ; //允许写入 tbEntry.Properties[ "AccessWrite" ][0] = false ; //脚本资源访问 tbEntry.Properties[ "AccessExecute" ][0] = false ; //允许匿名访问 tbEntry.Properties[ "AuthAnonymous" ][0] = true ; // 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证 tbEntry.Properties[ "AuthFlags" ][0] = 1; //允许基本验证 tbEntry.Properties[ "AuthBasic" ][0] = false ; //允许WIndows集成验证 tbEntry.Properties[ "AuthNTLM" ][0] = false ; //索引此资源 tbEntry.Properties[ "ContentIndexed" ][0] = false ; //目录浏览 tbEntry.Properties[ "EnableDirBrowsing" ][0] = false ; //脚本可执行 tbEntry.Properties[ "AccessScript" ][0] = true ; //允许父路径 tbEntry.Properties[ "AspEnableParentPaths" ][0] = true ; //应用程序名称 tbEntry.Properties[ "AppFriendlyName" ][0] = virtualDirName; //应用程序保护 tbEntry.Properties[ "AppIsolated" ][0] = 2; //设置默认文档 //tbEntry.Properties["DefaultDoc"][0] = "index.asp,index.html,index.htm"; tbEntry.Properties[ "EnableDefaultDoc" ][0] = true ; //日志访问 tbEntry.Properties[ "DontLog" ][0] = true ; ////用户名 //tbEntry.Properties["AnonymousUserName"][0] = "Administrator"; ////用户密码 //tbEntry.Properties["AnonymousUserPass"][0] = ""; ////程序池 //tbEntry.Properties["AppPoolId"][0] = appPoolId; tbEntry.CommitChanges(); root.CommitChanges(); return "1" ; } catch (Exception ex) { //return "0"; return ex.Message + "<br>" + ex.Source; } } |
使用
我们在项目运行的时候进行调用CreateVirtualDir函数,不过并不是所有的都需要进行创建虚拟目录。这里我在配置文件中设置了一个参数。用来判断是否开启虚拟目录。然后配置文件中也有设定的物理路径。仅供参考这种方式。
iis发布运行后,则可以通过自己创建的虚拟路径进行访问!如图:
结语
到这里就基本结束了。没有特别难的操作,但如果能和配置文件相结合设置的灵活 会非常的方便。减少了发布后繁琐的操作(不需要人手动去创建虚拟目录)。
以上代码有获取名字或者路径的方法更改为自己想要的名称就可以使用了。
到此这篇关于C# .NET创建虚拟目录的方法详解的文章就介绍到这了,更多相关C# .NET创建虚拟目录内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/BFMC/archive/2022/01/05/15765544.html