如何用C#新建一个IIS网站?

钛元素 2008-11-25 05:40:41
请看清要求:

是用c#建立IIS网站:

IIS为6.0,安装后有一个默认网站
利用代码新建一个与默认网站平级的网站

虚拟目录可以建立,但是网站不知道如何建...

help
...全文
396 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
钛元素 2008-11-27
  • 打赏
  • 举报
回复
只有4楼的理解了我的意思,我只会建虚拟目录。呜呜,哪里有资料参考?msdn里没.xiexie jietiela
Fibona 2008-11-26
  • 打赏
  • 举报
回复
通过 DirectoryEntry 来建,(ADSI)也可以通过WMI来建,WMI我没有用过


代码段

//增加站点
DirectoryEntry Service = new DirectoryEntry("IIS://" + MachineName + "/W3SVC");
if (Service == null)
return false;
DirectoryEntry Item = Service.Children.Add("IIS的标识,一个数字", "IIsWebServer"); //这里可以通过获取IIS中所有站点的标识,然后取最大的标识加1来做,本人就是这样做的
Item.Properties["ServerComment"][0] = "IIS中站点名称";
Item.Properties["Serverbindings"].Add(IP + ":" + Port + ":" + HostName);
Item.Properties["AccessScript"][0] = true;
Item.Properties["AccessWrite"][0] = true;
Item.Properties["AccessSource"][0] = true;
Item.Properties["AccessRead"][0] = true;
Item.Properties["EnableDirBrowsing"][0] = true
Item.Properties["DefaultDoc"].Value = true;
Item.Properties["EnableDefaultDoc"][0] = "index.htm,index.html,list.htm,list.html,default.aspx,default.htm,default.html";
Item.Properties["ScriptMaps"].Value = _GetAspNetVersion();
//Item.Properties["AppFriendlyName"][0] = "2";
Item.Properties["AppIsolated"][0] = "2";
Item.Properties["AccessExecute"][0] = true;


DirectoryEntry root = Item.Children.Add("Root", "IIsWebVirtualDir");
root.Properties["path"][0] = 路径;
Service.CommitChanges();
Item.CommitChanges();
root.CommitChanges();



#region 获取程序版本设置字符串
/// <summary>
/// 获取程序版本设置字符串
/// </summary>
/// <returns></returns>
private string[] _GetAspNetVersion()
{
string SystemPath = System.Environment.SystemDirectory;
string WindowsPath = _GetParentFolderCode(SystemPath);
string FramworkPath = WindowsPath + @"microsoft.net\framework\v" + System.Environment.Version.ToString(3);

string ScriptMaps = @".asp," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cer," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.cdx," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.asa," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
.idc," + SystemPath + @"\inetsrv\httpodbc.dll,5,GET,POST
.shtm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST
.shtml," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST
.stm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST
.asax," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ascx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ashx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.asmx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.aspx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.axd," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.vsdisco," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rem," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.soap," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.config," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cs," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.csproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vbproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.webinfo," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.licx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.resources," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.xoml," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.rules," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.master," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.skin," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.compiled," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.browser," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.mdb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.jsl," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.vjsproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sitemap," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.msgx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.ad," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.dd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.cd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.adprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.lddprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdm," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.sdmDocument," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.svc," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
.mdf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.ldf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.java," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.exclude," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
.refresh," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG";

string[] ret = System.Text.RegularExpressions.Regex.Split(ScriptMaps, "\r\n");
for (int i = 0; i < ret.Length; i++)
{
ret[i] = ret[i].Trim();
}
return ret;
}
#endregion
acqy 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xiaoronghu 的回复:]
用的什么IDE啊?
[/Quote]

Visual Studio.NET 2002/2003
Visual Studio 2005/2008/2010 CTP
Borland Delphi

都可以
xiaoronghu 2008-11-25
  • 打赏
  • 举报
回复
用的什么IDE啊?
kugou123 2008-11-25
  • 打赏
  • 举报
回复
用IDE建立对应的Project就可以了。
哥本哈士奇 2008-11-25
  • 打赏
  • 举报
回复
先整个vs2005这样的工具,新建一个web site项目,这个才是楼主所说的用C#写的网站,写好后将IIS的虚拟目录指向你的这个项目目录通常就可以了,不过条件比较复杂所以也不好说,得根据你实际的环境,而且网站的发布有很多方法,IIS里也有一些说道.
希望我没有理解错楼主的意思.

110,539

社区成员

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

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

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