IISManager.cs
2.5 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.DirectoryServices;
namespace ServiceManager
{
/// <summary>
/// IISManager ժҪ˵
/// </summary>
public class IISManager
{
/// <summary>
/// Default constructor uses localhost as default server
/// </summary>
public IISManager()
{
_serverName = "localhost";
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="serverName">Name of the IIS Server</param>
public IISManager(string serverName)
{
_serverName = serverName;
}
/// <summary>
/// Connect to IISServer
/// </summary>
public void Connect()
{
try
{
_iisServer = new DirectoryEntry("IIS://" + _serverName + "/W3SVC/1");
}
catch (Exception e)
{
throw new Exception(": " + _serverName,e);
}
}
/// <summary>
/// Create a virtual directory
/// </summary>
/// <param name="nameDirectory">Name of the new virtual directory</param>
/// <param name="realPath">Path of the directory</param>
public bool CreateVirtualDirectory(string nameDirectory,string realPath)
{
DirectoryEntry folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);
try
{
DirectoryEntry newVirDir = folderRoot.Children.Add(nameDirectory,VirDirSchemaName);
newVirDir.CommitChanges();
// Set Properties
newVirDir.Properties["AccessRead"].Add(true);
newVirDir.Properties["Path"].Value = realPath;
// Create a Application
newVirDir.Invoke("AppCreate",true);
// Save Changes
newVirDir.CommitChanges();
folderRoot.CommitChanges();
_iisServer.CommitChanges();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Delete a virtual directory
/// </summary>
/// <param name="nameDirectory">Name of the delete virtual directory</param>
public bool DeleteVirtualDirectory(string nameDirectory)
{
DirectoryEntry folderRoot = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
try
{
DirectoryEntry deVirDir = folderRoot.Children.Find(nameDirectory,VirDirSchemaName);
folderRoot.Children.Remove(deVirDir);
folderRoot.CommitChanges();
_iisServer.CommitChanges();
}
catch
{
//throw new Exception(e.Message,e);
return false;
}
return true;
}
#region Properties
public string ServerName
{
get
{
return _serverName;
}
set
{
_serverName = value;
}
}
#endregion
public static string VirDirSchemaName = "IIsWebVirtualDir";
#region Private Members
private string _serverName;
private DirectoryEntry _iisServer;
#endregion
}
}