出处:http://www.cnblogs.com/wubiyu/archive/2007/07/06/807828.html
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
using System;
6
using System.Data;
7
using System.DirectoryServices;
8
using System.Collections;
9
10
namespace IISManage
11

{
12
/**////<summary>
13
/// IIS管理类,可以实现创建站点、虚拟目录,删除虚拟目录等 ///
14
/// </summary>
15
public class IISManager
16
{
17
18
private string _server, _website, _AnonymousUserPass, _AnonymousUserName;
19
private VirtualDirectories _virdirs;
20
protected System.DirectoryServices.DirectoryEntry rootfolder;
21
private bool _batchflag;
22
23
24
构造函数#region 构造函数
25
/**//// <summary>
26
/// 构造函数 ///
27
/// </summary>
28
public IISManager()
29
{
30
//默认情况下使用localhost,即访问本地机
31
_server = "localhost"; _website = "1"; _batchflag = false;
32
}
33
34
35
/**//// <summary>
36
/// 构造函数
37
/// </summary>
38
/// <param name="strServer">服务器</param>
39
public IISManager(string strServer)
40
{
41
_server = strServer;
42
_website = "1"; _batchflag = false;
43
}
44
45
46
/**//// <summary>
47
/// 构造函数
48
/// </summary>
49
/// <param name="strServer">服务器</param>
50
/// <param name="website">站点,每一个站点为1,第二个站点为2,依此类推</param>
51
public IISManager(string strServer, int website)
52
{
53
_server = strServer;
54
_website = website.ToString();
55
_batchflag = false;
56
}
57
#endregion #region 定义公共属性
58
59
60
/**//// <summary>
61
/// 匿名访问用户
62
/// </summary>
63
public string AnonymousUserName
64
{
65
get
{ return _AnonymousUserName; }
66
set
{ _AnonymousUserName = value; }
67
}
68
69
70
/**//// <summary>
71
/// 匿名访问用户密码
72
/// </summary>
73
public string AnonymousUserPass
74
{
75
get
{ return _AnonymousUserPass; }
76
set
{ _AnonymousUserPass = value; }
77
}
78
79
80
/**//// <summary>
81
/// 服务器,可以是IP或计算名
82
/// </summary>
83
public string Server
84
{
85
get
{ return _server; }
86
set
{ _server = value; }
87
}
88
89
/**//// <summary>
90
/// 站点,一般来说第一台主机为1,第二台主机为2,依次类推
91
/// </summary>
92
public int WebSite
93
{
94
get
{ return Convert.ToInt32(_website); }
95
set
{ _website = Convert.ToString(value); }
96
}
97
98
99
/**//// <summary>
100
/// 虚拟目录的名字
101
/// </summary>
102
public VirtualDirectories VirDirs
103
{
104
get
{ return _virdirs; }
105
set
{ _virdirs = value; }
106
}
107
108
109
110
定义公共方法#region 定义公共方法
111
/**//// <summary>
112
/// 获取匿名访问用户的用户名和密码
113
/// </summary>
114
public void get_AnonymousUser()
115
{
116
_AnonymousUserPass = "IUSR_DEVE-SERVER";
117
_AnonymousUserName = "IUSR_DEVE-SERVER";
118
VirtualDirectory vDir;
119
try
120
{
121
Hashtable myList = (Hashtable)_virdirs;
122
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
123
while (myEnumerator.MoveNext())
124
{
125
vDir = (VirtualDirectory)myEnumerator.Value;
126
if (vDir.AnonymousUserName != "" && vDir.AnonymousUserPass != "")
127
{
128
_AnonymousUserName = vDir.AnonymousUserName;
129
_AnonymousUserPass = vDir.AnonymousUserPass;
130
break;
131
}
132
}
133
}
134
catch
135
{
136
_AnonymousUserPass = "IUSR_DEVE-SERVER";
137
_AnonymousUserName = "IUSR_DEVE-SERVER";
138
}
139
}
140
141
142
/**//// <summary>
143
/// 连接服务器
144
/// </summary>
145
public void Connect()
146
{
147
ConnectToServer();
148
}
149
/**//// <summary>
150
/// 连接服务器
151
/// </summary>
152
/// <param name="strServer">服务器名称</param>
153
public void Connect(string strServer)
154
{
155
_server = strServer;
156
ConnectToServer();
157
}
158
/**//// <summary>
159
/// 连接服务器
160
/// </summary>
161
/// <param name="strServer">服务器名称</param>
162
/// <param name="strWebSite">站点,一般来说第一台主机为1,第二台主机为2,依次类推</param>
163
public void Connect(string strServer, string strWebSite)
164
{
165
_server = strServer;
166
_website = strWebSite;
167
ConnectToServer();
168
}
169
170
171
/**//// <summary>
172
/// 添加一个站点
173
/// </summary>
174
/// <param name="path">站点的路径</param>
175
public void CreateWebSite(string webSiteName, string port, string path)
176
{
177
try
178
{
179
DirectoryEntry root = new DirectoryEntry("IIS://" + this._server + "/W3SVC");
180
int siteID = 1;
181
foreach (DirectoryEntry e in root.Children)
182
{
183
if (e.SchemaClassName == "IIsWebServer")
184
{
185
int ID = Convert.ToInt32(e.Name);
186
if (ID >= siteID)
{ siteID = ID + 1; }
187
if (e.Properties["ServerBindings"][0].ToString() == ":" + port + ":")
188
{
189
throw new Exception(port + "已被占用,请选择其它端口!");
190
}
191
}
192
}
193
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
194
site.Invoke("Put", "ServerComment", webSiteName); site.Invoke("Put", "KeyType", "IIsWebServer");
195
site.Invoke("Put", "ServerBindings", ":" + port + ":"); site.Invoke("Put", "ServerState", 2);
196
site.Invoke("Put", "FrontPageWeb", 1); site.Invoke("Put", "DefaultDoc", "index.aspx");
197
site.Invoke("Put", "SecureBindings", ":443:"); site.Invoke("Put", "ServerAutoStart", 1);
198
site.Invoke("Put", "ServerSize", 1); site.Invoke("SetInfo");
199
DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
200
siteVDir.Properties["AppIsolated"][0] = 2;
201
siteVDir.Properties["Path"][0] = path;
202
siteVDir.Properties["AccessFlags"][0] = 513;
203
siteVDir.Properties["FrontPageWeb"][0] = 1;
204
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
205
siteVDir.Properties["AppFriendlyName"][0] = "ROOT";
206
siteVDir.CommitChanges(); site.CommitChanges();
207
}
208
catch (Exception ex)
209
{
210
throw ex;
211
}
212
}
213
214
215
/**//// <summary>
216
/// 取得所有站点
217
/// </summary>
218
/// <returns></returns>
219
public System.Collections.Hashtable getSites()
220
{
221
try
222
{
223
DirectoryEntry root = new DirectoryEntry("IIS://" + this._server + "/W3SVC");
224
Hashtable sites = new Hashtable();
225
foreach (DirectoryEntry e in root.Children)
226
{
227
if (e.SchemaClassName == "IIsWebServer")
228
{
229
sites.Add(e.Name, e);
230
}
231
}
232
return sites;
233
}
234
catch (Exception ex)
235
{
236
throw ex;
237
}
238
}
239
240
241
/**//// <summary>
242
/// 判断是否存这个虚拟目录
243
/// </summary>
244
/// <param name="strVirdir">虚拟目录名称</param>
245
/// <returns></returns>
246
public bool Exists(string strVirdir)
247
{
248
return _virdirs.Contains(strVirdir);
249
}
250
/**//// <summary>
251
/// 添加一个虚拟目录
252
/// </summary>
253
/// <param name="newdir">虚拟目录</param>
254
/// <returns></returns>
255
public bool CreateVirtualDirectory(VirtualDirectory newdir)
256
{
257
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" + newdir.Name;
258
if (!_virdirs.Contains(newdir.Name) || _batchflag)
259
{
260
try
261
{
262
//加入到ROOT的Children集合中去
263
DirectoryEntry newVirDir = rootfolder.Children.Add(newdir.Name, "IIsWebVirtualDir");
264
newVirDir.Invoke("AppCreate", true);
265
newVirDir.CommitChanges();
266
rootfolder.CommitChanges();
267
//然后更新数据
268
UpdateDirInfo(newVirDir, newdir);
269
return true;
270
}
271
catch (Exception)
272
{
273
return false;
274
}
275
}
276
else
277
{
278
return false;
279
}
280
}
281
/**//// <summary>
282
/// 得到一个虚拟目录
283
/// </summary>
284
/// <param name="strVirdir">虚拟目录名</param>
285
/// <returns></returns>
286
public VirtualDirectory GetVirDir(string strVirdir)
287
{
288
VirtualDirectory tmp = null;
289
if (_virdirs.Contains(strVirdir))
290
{
291
tmp = _virdirs.Find(strVirdir);
292
((VirtualDirectory)_virdirs[strVirdir]).flag = 2;
293
}
294
else
295
{
296
throw new Exception("虚拟目录" + strVirdir + "不存在!");
297
}
298
return tmp;
299
}
300
301
302
/**//// <summary>
303
/// 更新一个虚拟目录
304
/// </summary>
305
/// <param name="dir">虚拟目录</param>
306
public void Update(VirtualDirectory dir)
307
{
308
//判断需要更改的虚拟目录是否存在
309
if (_virdirs.Contains(dir.Name))
310
{
311
DirectoryEntry ode = rootfolder.Children.Find(dir.Name, "IIsWebVirtualDir");
312
UpdateDirInfo(ode, dir);
313
}
314
else
315
{
316
System.Windows.Forms.MessageBox.Show("虚拟目录" + dir.Name + "不存在!");
317
}
318
}
319
/**//// <summary>
320
/// 删除一个虚拟目录
321
/// </summary>
322
/// <param name="strVirdir">虚拟目录名</param>
323
public void Delete(string strVirdir)
324
{
325
if (_virdirs.Contains(strVirdir))
326
{
327
object[] paras = new object[2];
328
paras[0] = "IIsWebVirtualDir";
329
//表示操作的是虚拟目录
330
paras[1] = strVirdir;
331
rootfolder.Invoke("Delete", paras);
332
rootfolder.CommitChanges();
333
}
334
else
335
{
336
System.Windows.Forms.MessageBox.Show("虚拟目录" + strVirdir + "不存在!");
337
}
338
}
339
/**//// <summary>
340
/// 批量更新
341
/// </summary>
342
public void UpdateBatch()
343
{
344
BatchUpdate(_virdirs);
345
}
346
347
348
/**//// <summary>
349
/// 批量更新
350
/// </summary>
351
/// <param name="vds">虚拟目录集合</param>
352
public void UpdateBatch(VirtualDirectories vds)
353
{
354
BatchUpdate(vds);
355
}
356
357
358
/**//// <summary>
359
/// 获取虚拟目录集合
360
/// </summary>
361
/// <returns></returns>
362
public VirtualDirectories GetVirtualDirdctories()
363
{
364
VirtualDirectories vds = GetVirDirs(this.rootfolder.Children);
365
return vds;
366
}
367
#endregion
368
369
私有方法#region 私有方法
370
371
372
/**//// <summary>
373
/// 关闭当前对象
374
/// </summary>
375
public void Close()
376
{
377
_virdirs.Clear();
378
_virdirs = null;
379
rootfolder.Dispose();
380
}
381
/**//// <summary>
382
/// 连接服务器
383
/// </summary>
384
private void ConnectToServer()
385
{
386
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT";
387
try
388
{
389
this.rootfolder = new DirectoryEntry(strPath);
390
_virdirs = GetVirDirs(this.rootfolder.Children);
391
}
392
catch (Exception)
393
{
394
System.Windows.Forms.MessageBox.Show("无法连接到服务器:" + _server);
395
}
396
}
397
/**//// <summary>
398
/// 执行批量更新
399
/// </summary>
400
/// <param name="vds">虚拟目录集合</param>
401
private void BatchUpdate(VirtualDirectories vds)
402
{
403
_batchflag = true;
404
foreach (object item in vds.Values)
405
{
406
VirtualDirectory vd = (VirtualDirectory)item;
407
switch (vd.flag)
408
{
409
case 0:
410
break;
411
case 1:
412
CreateVirtualDirectory(vd);
413
break;
414
case 2:
415
Update(vd);
416
break;
417
}
418
}
419
_batchflag = false;
420
}
421
/**//// <summary>
422
/// 更新指定虚拟目录
423
/// </summary>
424
/// <param name="de"></param>
425
/// <param name="vd">要执行更新的虚拟目录</param>
426
private void UpdateDirInfo(DirectoryEntry de, VirtualDirectory vd)
427
{
428
de.Properties["AnonymousUserName"][0] = vd.AnonymousUserName;
429
de.Properties["AnonymousUserPass"][0] = vd.AnonymousUserPass;
430
de.Properties["AccessRead"][0] = vd.AccessRead;
431
de.Properties["AccessExecute"][0] = vd.AccessExecute;
432
de.Properties["AccessWrite"][0] = vd.AccessWrite;
433
de.Properties["AuthBasic"][0] = vd.AuthBasic;
434
de.Properties["AuthNTLM"][0] = vd.AuthNTLM;
435
de.Properties["ContentIndexed"][0] = vd.ContentIndexed;
436
de.Properties["EnableDefaultDoc"][0] = vd.EnableDefaultDoc;
437
de.Properties["EnableDirBrowsing"][0] = vd.EnableDirBrowsing;
438
de.Properties["AccessSSL"][0] = vd.AccessSSL;
439
de.Properties["AccessScript"][0] = vd.AccessScript;
440
de.Properties["DefaultDoc"][0] = vd.DefaultDoc;
441
de.Properties["Path"][0] = vd.Path;
442
de.CommitChanges();
443
}
444
445
446
/**//// 获取虚拟目录集合
447
/// </summary>
448
/// <param name="des"></param>
449
/// <returns></returns>
450
private VirtualDirectories GetVirDirs(DirectoryEntries des)
451
{
452
VirtualDirectories tmpdirs = new VirtualDirectories();
453
foreach (DirectoryEntry de in des)
454
{
455
if (de.SchemaClassName == "IIsWebVirtualDir")
456
{
457
VirtualDirectory vd = new VirtualDirectory();
458
vd.Name = de.Name;
459
vd.AccessRead = (bool)de.Properties["AccessRead"][0];
460
vd.AccessExecute = (bool)de.Properties["AccessExecute"][0];
461
vd.AccessWrite = (bool)de.Properties["AccessWrite"][0];
462
vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0];
463
vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0];
464
vd.AuthBasic = (bool)de.Properties["AuthBasic"][0];
465
vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0];
466
vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0];
467
vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0];
468
vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0];
469
vd.AccessSSL = (bool)de.Properties["AccessSSL"][0];
470
vd.AccessScript = (bool)de.Properties["AccessScript"][0];
471
vd.Path = (string)de.Properties["Path"][0];
472
vd.flag = 0; vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0];
473
tmpdirs.Add(vd.Name, vd);
474
}
475
}
476
return tmpdirs;
477
}
478
#endregion
479
}
480
/**//// <summary>
481
/// 虚拟目录实体
482
/// </summary>
483
public class VirtualDirectory
484
{
485
private bool _read, _execute, _script, _ssl, _write, _authbasic, _authntlm, _indexed, _endirbrow, _endefaultdoc;
486
private string _ausername, _auserpass, _name, _path;
487
private int _flag;
488
private string _defaultdoc;
489
490
/**//// <summary>
491
/// 构造函数
492
/// </summary>
493
public VirtualDirectory()
494
{
495
SetValue();
496
}
497
498
499
/**//// <summary>
500
/// 构造函数
501
/// </summary>
502
/// <param name="sVirDirName">虚拟目录名</param>
503
public VirtualDirectory(string sVirDirName)
504
{
505
SetValue();
506
_name = sVirDirName;
507
}
508
/**//// <summary>
509
/// 构造函数
510
/// </summary>
511
/// <param name="sVirDirName">虚拟目录名</param>
512
/// <param name="strPhyPath">物理路径</param>
513
/// <param name="AnonymousUser"></param>
514
public VirtualDirectory(string sVirDirName, string strPhyPath, string[] AnonymousUser)
515
{
516
SetValue();
517
_name = sVirDirName;
518
_path = strPhyPath;
519
_ausername = AnonymousUser[0];
520
_auserpass = AnonymousUser[1];
521
}
522
523
524
525
/**//// <summary>
526
/// 设置虚拟目录的属性
527
/// </summary>
528
private void SetValue()
529
{
530
_read = true;
531
_execute = false;
532
_script = true;
533
_ssl = false;
534
_write = false;
535
_authbasic = false;
536
_authntlm = true;
537
_indexed = true;
538
_endirbrow = false;
539
_endefaultdoc = true;
540
_flag = 1;
541
_defaultdoc = "index.aspx";
542
_path = "C://";
543
_ausername = "IUSR_DEVE-SERVER";
544
_auserpass = "IUSR_DEVE-SERVER";
545
_name = "";
546
}
547
548
549
public override string ToString()
550
{
551
return this._name;
552
}
553
554
定义属性#region 定义属性
555
public int flag
556
{
557
get
{ return _flag; }
558
set
{ _flag = value; }
559
}
560
public bool AccessRead
561
{
562
get
{ return _read; }
563
set
{ _read = value; }
564
}
565
public bool AccessWrite
566
{
567
get
{ return _write; }
568
set
{ _write = value; }
569
}
570
public bool AccessExecute
571
{
572
get
{ return _execute; }
573
set
{ _execute = value; }
574
}
575
public bool AccessSSL
576
{
577
get
{ return _ssl; }
578
set
{ _ssl = value; }
579
}
580
public bool AccessScript
581
{
582
get
{ return _script; }
583
set
{ _script = value; }
584
}
585
public bool AuthBasic
586
{
587
get
{ return _authbasic; }
588
set
{ _authbasic = value; }
589
}
590
public bool AuthNTLM
591
{
592
get
{ return _authntlm; }
593
set
{ _authntlm = value; }
594
}
595
public bool ContentIndexed
596
{
597
get
{ return _indexed; }
598
set
{ _indexed = value; }
599
}
600
public bool EnableDirBrowsing
601
{
602
get
{ return _endirbrow; }
603
set
{ _endirbrow = value; }
604
}
605
public bool EnableDefaultDoc
606
{
607
get
{ return _endefaultdoc; }
608
set
{ _endefaultdoc = value; }
609
}
610
public string Name
611
{
612
get
{ return _name; }
613
set
{ _name = value; }
614
}
615
public string Path
616
{
617
get
{ return _path; }
618
set
{ _path = value; }
619
}
620
public string DefaultDoc
621
{
622
get
{ return _defaultdoc; }
623
set
{ _defaultdoc = value; }
624
}
625
public string AnonymousUserName
626
{
627
get
{ return _ausername; }
628
set
{ _ausername = value; }
629
}
630
public string AnonymousUserPass
631
{
632
get
{ return _auserpass; }
633
set
{ _auserpass = value; }
634
}
635
#endregion
636
}
637
638
639
/**//// <summary>
640
/// 虚拟目录集合类
641
/// </summary>
642
public class VirtualDirectories : System.Collections.Hashtable
643
{
644
public VirtualDirectories()
{ }
645
/**//// <summary>
646
/// 搜索指定的虚拟目录
647
/// </summary>
648
/// <param name="strName">虚拟目录名</param>
649
/// <returns></returns>
650
public VirtualDirectory Find(string strName)
651
{
652
return (VirtualDirectory)this[strName];
653
}
654
}
655
}
656
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



121

122

123

124



125

126

127



128

129

130

131

132

133

134

135



136

137

138

139

140

141

142


143

144

145

146



147

148

149


150

151

152

153

154



155

156

157

158


159

160

161

162

163

164



165

166

167

168

169

170

171


172

173

174

175

176



177

178



179

180

181

182



183

184



185

186



187

188



189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209



210

211

212

213

214

215


216

217

218

219

220



221

222



223

224

225

226



227

228



229

230

231

232

233

234

235



236

237

238

239

240

241


242

243

244

245

246

247



248

249

250


251

252

253

254

255

256



257

258

259



260

261



262

263

264

265

266

267

268

269

270

271

272



273

274

275

276

277



278

279

280

281


282

283

284

285

286

287



288

289

290



291

292

293

294

295



296

297

298

299

300

301

302


303

304

305

306

307



308

309

310



311

312

313

314

315



316

317

318

319


320

321

322

323

324



325

326



327

328

329

330

331

332

333

334

335



336

337

338

339


340

341

342

343



344

345

346

347

348


349

350

351

352

353



354

355

356

357

358


359

360

361

362

363



364

365

366

367

368

369


370

371

372


373

374

375

376



377

378

379

380

381


382

383

384

385



386

387

388



389

390

391

392

393



394

395

396

397


398

399

400

401

402



403

404

405



406

407

408



409

410

411

412

413

414

415

416

417

418

419

420

421


422

423

424

425

426

427



428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446


447

448

449

450

451



452

453

454



455

456



457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480


481

482

483

484



485

486

487

488

489

490


491

492

493

494



495

496

497

498

499


500

501

502

503

504



505

506

507

508


509

510

511

512

513

514

515



516

517

518

519

520

521

522

523

524

525


526

527

528

529



530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550



551

552

553

554


555

556



557



558



559

560

561



562



563



564

565

566



567



568



569

570

571



572



573



574

575

576



577



578



579

580

581



582



583



584

585

586



587



588



589

590

591



592



593



594

595

596



597



598



599

600

601



602



603



604

605

606



607



608



609

610

611



612



613



614

615

616



617



618



619

620

621



622



623



624

625

626



627



628



629

630

631



632



633



634

635

636

637

638

639


640

641

642

643



644



645


646

647

648

649

650

651



652

653

654

655

656
