3

我开发了一个程序“安装程序”,它将在 IIS 上添加和配置一个新网站。我的问题是我想在安装之前检查用户提供的端口是否已被另一个站点使用。

我的项目是 C# WinForm 风格的。有人你有想法吗?

string[] lPorts = System.IO.Ports.SerialPort.GetPortNames(); ?
4

3 回答 3

4

检查这个:

BindingInformation 属性:获取或设置当前绑定的绑定信息。

此属性的值是一个以冒号分隔的字符串,其中包括绑定的 IP 地址、端口和主机名。您可以将主机名留空。您可以将 IP 地址设置为“*”以指示绑定适用于所有变量。

例如,为端口 80 上的所有 IP 地址设置且没有指定主机名的绑定从该属性返回“:80:”。在端口 8080 上为 IP 地址 192.168.1.150 设置的绑定返回“192.168.1.150:8080:”。为名为“microsoft.com”的主机的端口 80 上的所有 IP 地址设置的绑定返回“ :80:microsoft.com”。

BindingInformation 属性值在 ApplicationHost.config 文件中维护。

此外,您可以检查以下内容: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);
                }
            }
        }
于 2013-01-07T11:16:18.903 回答
1

您应该查看 Microsoft.Web.Administration命名空间

您应该使用ServerManager.Sites来获取网站列表。查看每个站点上的集合绑定。每个站点可能有一个或多个绑定,即可以通过一个或多个地址/端口访问它。

您上面的代码查找在 USB 之前用于连接调制解调器和打印机的物理串行端口。

希望这可以帮助!

于 2013-01-07T11:06:24.303 回答
0

不确定你是否在寻找 C#,但如果你需要 powershell,你就去吧..

打开 PowerShell 并输入以下内容: import-module webadministration get-webbinding

于 2014-08-26T01:02:10.710 回答