如何通过程序在IIS中建一个虚拟站点(VirtualServer)

symble 2004-08-10 02:33:48
如何通过程序在IIS中建一个虚拟站点(VirtualServer),最好是能用c#去建。谢谢先。
...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
m777 2004-08-10
  • 打赏
  • 举报
回复
创建虚拟路径的类,自己研究吧:
using System;
using System.DirectoryServices;

namespace VirtualDirectory
{
/// <summary>
/// Summary description for IISManager.
/// </summary>
public class IISManager
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="serverName">Name of the IIS Server</param>
public IISManager(string serverName)
{
_serverName = serverName;
}
/// <summary>
/// Default constructor uses localhost as default server
/// </summary>
public IISManager()
{
_serverName = "localhost";
}

/// <summary>
/// Connect to IISServer
/// </summary>
public void Connect()
{
try
{
_iisServer = new DirectoryEntry("IIS://" + _serverName + "/W3SVC/1");
}
catch (Exception e)
{
throw new Exception("Could not connect to: " + _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 void CreateVirtualDirectory(string nameDirectory,string realPath)
{
DirectoryEntry folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);
try
{
DirectoryEntry newVirDir = folderRoot.Children.Add(nameDirectory,VirDirSchemaName);
// Set Properties
newVirDir.Properties["AccessRead"].Add(true);
newVirDir.Properties["Path"].Add(realPath);
// Create a Application
newVirDir.Invoke("AppCreate",true);
// Save Changes
newVirDir.CommitChanges();
folderRoot.CommitChanges();
_iisServer.CommitChanges();
}
catch (Exception e)
{
throw new Exception("Virtual Directory " + nameDirectory + " Already Exists",e);
}
}

#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
}


}
孟子E章 2004-08-10
  • 打赏
  • 举报
回复
http://www.c-sharpcorner.com/Internet/CreatingWebServerInCSIMA.asp
http://www.c-sharpcorner.com/Code/2002/July/CreateVirtualDirs.asp
http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx

必须有管理员权限

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧