如何在代码后面检查端口当前是否与IIS7(webSite)一起使用? [英] how check if port is currently used with IIS7 (webSite) in code behind?
问题描述
我开发了一个程序安装程序,它将在IIS上添加和配置一个新网站。
我的问题是我想在安装之前检查用户提供的端口是否已被其他网站使用。
I developed a program "installer" that will add and configure a new website on IIS. My problem is that I want to check if the port given by the user is already used by another site before installing.
我的项目在C#WinForm中样式。
有人知道吗?
My project is in C # WinForm style. Someone you have an idea?
string[] lPorts = System.IO.Ports.SerialPort.GetPortNames(); ?
推荐答案
检查:
BindingInformation属性:获取或设置当前绑定的绑定信息。
BindingInformation Property: Gets or sets the binding information for the current binding.
此属性的值是以冒号分隔的字符串,包括绑定的IP地址,端口和主机名。您可以将主机名留空。您可以将IP地址设置为*以指示绑定适用于所有变量。
The value of this property is a colon-delimited string that includes the IP address, port, and host name of the binding. You can leave the host name blank. You can set the IP address to "*" to indicate that the binding works for all variables.
例如,为端口80上的所有IP地址设置的绑定并且没有指定的主机名从此属性返回:80:。在端口8080上为IP地址192.168.1.150设置的绑定将返回192.168.1.150:8080:。为端口80上的所有IP地址设置的绑定为名为microsoft.com的主机返回:80:microsoft.com。
For example, a binding that is set for all IP addresses on port 80 and has no specified host name returns ":80:" from this property. A binding that is set for IP address 192.168.1.150 on port 8080 returns "192.168.1.150:8080:". A binding that is set for all IP addresses on port 80 for a host named "microsoft.com" returns ":80:microsoft.com".
BindingInformation属性值在ApplicationHost.config文件中维护。
The BindingInformation property values are maintained in the ApplicationHost.config file.
此外,您可以检查: 在运行时获取IIS绑定
Also, you can check this: Get IIS bindings at runtime
foreach (Microsoft.Web.Administration.ConfigurationElement binding in mySite.GetCollection("bindings"))
{
string protocol = (string)binding["protocol"];
string bindingInfo = (string)binding["bindingInformation"];
if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
string[] parts = bindingInfo.Split(':');
if (parts.Length == 3)
{
//Get the port in use HERE !!!
string port = parts[1];
yield return new KeyValuePair<string, string>(protocol, port);
}
}
}
这篇关于如何在代码后面检查端口当前是否与IIS7(webSite)一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!