how check if port is currently used with IIS7 (webSite) in code behind?

五迷三道 提交于 2019-12-13 13:46:43

问题


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.

My project is in C # WinForm style. Someone you have an idea?

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

回答1:


Check this:

BindingInformation Property: Gets or sets the binding information for the current binding.

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.

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".

The BindingInformation property values are maintained in the ApplicationHost.config file.

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);
                }
            }
        }



回答2:


You should look in the Microsoft.Web.Administration namespace

You should use ServerManager.Sites to get a listing of sites. Take a look at the collection Bindings on each site. Each site may have one or more bindings, i.e. it may be accessed via one or more addresses / ports.

Your code above looks for the physical serial ports which where used before usb to connect modems and printers.

Hope this helps!




回答3:


Not sure if you are looking for C#, but if you need powershell here you go..

Open PowerShell and enter the following: import-module webadministration get-webbinding



来源:https://stackoverflow.com/questions/14194394/how-check-if-port-is-currently-used-with-iis7-website-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!