C# 使用C和设置端口号在IIS中以编程方式创建网站
我们已经能够创建一个网站。我们使用此链接中的信息进行了此操作: 但是,我们希望使用端口号80以外的端口号。我们如何做到这一点 我们正在使用iis6 在站点属性中,选择“网站”选项卡并指定TCP端口。C# 使用C和设置端口号在IIS中以编程方式创建网站,c#,iis,iis-6,port,C#,Iis,Iis 6,Port,我们已经能够创建一个网站。我们使用此链接中的信息进行了此操作: 但是,我们希望使用端口号80以外的端口号。我们如何做到这一点 我们正在使用iis6 在站点属性中,选择“网站”选项卡并指定TCP端口。 在studio中指定调试目的http://localhost:/如果您使用的是IIS 7,则有一个名为 以上博客文章中的一个例子: ServerManager iisManager = new ServerManager(); iisManager.Sites.Add("NewSite", "htt
在studio中指定调试目的http://localhost:/如果您使用的是IIS 7,则有一个名为 以上博客文章中的一个例子:
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
iisManager.CommitChanges();
如果您使用的是IIS 6,并且希望这样做,那么不幸的是,它更复杂
您必须在每台服务器上创建一个web服务,一个处理网站创建的web服务,因为如果我没有记错的话,通过网络进行的直接用户模拟将无法正常工作
您必须使用互操作服务并执行与此类似的操作。此示例使用两个对象server和site,它们是存储服务器和站点配置的自定义类的实例:
string metabasePath = "IIS://" + server.ComputerName + "/W3SVC";
DirectoryEntry w3svc = new DirectoryEntry(metabasePath, server.Username, server.Password);
string serverBindings = ":80:" + site.HostName;
string homeDirectory = server.WWWRootPath + "\\" + site.FolderName;
object[] newSite = new object[] { site.Name, new object[] { serverBindings }, homeDirectory };
object websiteId = (object)w3svc.Invoke("CreateNewSite", newSite);
// Returns the Website ID from the Metabase
int id = (int)websiteId;
请参阅更多信息尝试以下代码以了解未使用的端口号
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// Find unused ID PortNo for new web site
bool found_valid_port_no = false;
int random_port_no = 1;
do
{
bool regenerate_port_no = false;
System.Random random_generator = new Random();
random_port_no = random_generator.Next(9000,15000);
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int site_id = Convert.ToInt32(e.Name);
//For each detected ID find the port Number
DirectoryEntry vRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + site_id);
PropertyValueCollection pvcServerBindings = vRoot.Properties["serverbindings"];
String bindings = pvcServerBindings.Value.ToString().Replace(":", "");
int port_no = Convert.ToInt32(bindings);
if (port_no == random_port_no)
{
regenerate_port_no = true;
break;
}
}
}
found_valid_port_no = !regenerate_port_no;
} while (!found_valid_port_no);
int newportId = random_port_no;
这是解决办法。
点击按钮:
try
{
ServerManager serverMgr = new ServerManager();
string strWebsitename = txtwebsitename.Text; // abc
string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS
string strhostname = txthostname.Text; //abc.com
string stripaddress = txtipaddress.Text;// ip address
string bindinginfo = stripaddress + ":80:" + strhostname;
//check if website name already exists in IIS
Boolean bWebsite = IsWebsiteExists(strWebsitename);
if (!bWebsite)
{
Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:\\inetpub\\wwwroot\\yourWebsite");
mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
serverMgr.CommitChanges();
lblmsg.Text = "New website " + strWebsitename + " added sucessfully";
}
else
{
lblmsg.Text = "Name should be unique, " + strWebsitename + " is already exists. ";
}
}
catch (Exception ae)
{
Response.Redirect(ae.Message);
}
在站点上循环是否已存在名称
public bool IsWebsiteExists(string strWebsitename)
{
Boolean flagset = false;
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
flagset = true;
break;
}
else
{
flagset = false;
}
}
return flagset;
}
我已经看完了这里的所有答案,也进行了测试。下面是这个问题最简洁、最聪明的答案。但是,这仍然无法在IIS 6.0上工作。因此,需要IIS 8.0或更高版本
string domainName = "";
string appPoolName = "";
string webFiles = "C:\\Users\\John\\Desktop\\New Folder";
if (IsWebsiteExists(domainName) == false)
{
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add(domainName, "http", "*:8080:", webFiles);
iisManager.ApplicationDefaults.ApplicationPoolName = appPoolName;
iisManager.CommitChanges();
}
else
{
Console.WriteLine("Name Exists already");
}
public static bool IsWebsiteExists(string strWebsitename)
{
ServerManager serverMgr = new ServerManager();
Boolean flagset = false;
SiteCollection sitecollection = serverMgr.Sites;
flagset = sitecollection.Any(x => x.Name == strWebsitename);
return flagset;
}
您使用的IIS版本是什么?您想在安装过程中指定端口,还是想通过代码将网站添加到IIS?@Wael将网站添加到IIS,同时指定该网站的端口号。感谢您的回复。但这是使用UI的方式,我需要知道如何在代码中执行。您可以在JScript或VBScript上使用wmi吗?谢谢,不幸的是,我们正在使用IIS 6。我曾经在旧版维护项目中执行过此操作,我将看看是否能找到代码。不幸的是,它非常神秘和复杂。太好了。。。顺便说一句,注意应用程序池。。。可能需要一些额外的配置…仅适用于IIS7。。找不到IIS6的参考,请查看我的答案。这可能更聪明way@AdamDrewery没有帮助。他工作的地方可能需要它。@LiakatHossain虽然我更喜欢你的lambda风格,但逻辑完全一样。