markus / ConvertService / ConverterService / DZConverterLib / VirtualDirCreate.cs @ e7cb3fab
이력 | 보기 | 이력해설 | 다운로드 (4.39 KB)
1 | 7ca218b3 | KangIngu | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.DirectoryServices; |
||
6 | |||
7 | namespace DZConverterLib |
||
8 | { |
||
9 | public class VirtualDirCreate |
||
10 | { |
||
11 | /// <summary> |
||
12 | /// |
||
13 | /// </summary> |
||
14 | /// <param name="serverName"></param> |
||
15 | /// <param name="website"></param> |
||
16 | /// <param name="appName"></param> |
||
17 | /// <param name="path"></param> |
||
18 | public static void CreateVirtualDir(string serverName, string website, string appName, string path) |
||
19 | { |
||
20 | DirectoryEntry IISSchema = new DirectoryEntry(string.Concat("IIS://", serverName, "/Schema/AppIsolated")); |
||
21 | bool canCreate = IISSchema.Properties["Syntax"].Value.ToString().ToUpper() != "BOOLEAN"; |
||
22 | IISSchema.Dispose(); |
||
23 | |||
24 | //get the identifier for the site we want |
||
25 | int identifier = 0; |
||
26 | DirectoryEntry root = new DirectoryEntry(string.Concat("IIS://", serverName, "/W3SVC")); |
||
27 | |||
28 | foreach(DirectoryEntry de in root.Children) |
||
29 | { |
||
30 | if (de.SchemaClassName.ToUpper().Equals("IISWEBSERVER") |
||
31 | &&de.Invoke("Get", "ServerComment").ToString().ToUpper().Equals(website.ToUpper())) |
||
32 | { |
||
33 | identifier = Convert.ToInt32(de.Name); |
||
34 | break; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | if (canCreate && identifier > 0) |
||
39 | { |
||
40 | bool pathCreated = false; |
||
41 | |||
42 | try |
||
43 | { |
||
44 | DirectoryEntry iisAdmin = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/{1}/Root", serverName, identifier)); |
||
45 | //make sure folder exists |
||
46 | |||
47 | if (!System.IO.Directory.Exists(path)) |
||
48 | { |
||
49 | System.IO.Directory.CreateDirectory(path); |
||
50 | pathCreated = true; |
||
51 | } |
||
52 | //If the virtual directory already exists then delete it |
||
53 | foreach (DirectoryEntry vd in iisAdmin.Children) |
||
54 | { |
||
55 | if (vd.Name.Equals(appName)) |
||
56 | { |
||
57 | iisAdmin.Invoke("Delete", new string[] { vd.SchemaClassName, appName }); |
||
58 | iisAdmin.CommitChanges(); |
||
59 | break; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | //Create and setup new virtual directory |
||
64 | DirectoryEntry vDir = iisAdmin.Children.Add(appName, "IIsWebVirtualDir"); |
||
65 | vDir.Properties["Path"][0] = path; |
||
66 | vDir.Properties["AppFriendlyName"][0] = appName; |
||
67 | vDir.Properties["EnableDirBrowsing"][0] = false; |
||
68 | vDir.Properties["AccessRead"][0] = true; |
||
69 | vDir.Properties["AccessExecute"][0] = true; |
||
70 | vDir.Properties["AccessWrite"][0] = false; |
||
71 | vDir.Properties["AccessScript"][0] = true; |
||
72 | vDir.Properties["AuthNTLM"][0] = true; |
||
73 | vDir.Properties["EnableDefaultDoc"][0] = true; |
||
74 | vDir.Properties["DefaultDoc"][0] = "default.htm,default.aspx,default.asp"; |
||
75 | vDir.Properties["AspEnableParentPaths"][0] = true; |
||
76 | vDir.CommitChanges(); |
||
77 | //the following are acceptable params |
||
78 | //INPROC = 0 |
||
79 | //OUTPROC = 1 |
||
80 | //POOLED = 2 |
||
81 | |||
82 | vDir.Invoke("AppCreate", 1); |
||
83 | } |
||
84 | catch (Exception ex) |
||
85 | { |
||
86 | if (pathCreated) |
||
87 | { |
||
88 | System.IO.Directory.Delete(path); |
||
89 | } |
||
90 | |||
91 | throw ex; |
||
92 | } |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | throw new ApplicationException("Failed to create Virtual Directory"); |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |