Check for available port when installing using inn

2019-07-17 12:33发布

问题:

I am trying to create the following functionality in inno setup.

The user is asked to enter a port they wish my application to communicate on. Once they have entered a port they can hit a check button. This check button will run some code to see whether the port on the installation machine is available.

So far I am fine with creating the input box for the user to enter the port they wish to test. I have the following code to test whether the port is available.

int port = 456; //<--- This is your value
bool isAvailable = true;

// Evaluate current system tcp connections. This is the same information provided
// by the netstat command line application, just in .Net strongly-typed object
// form.  We will look through the list, and if our port we would like to use
// in our TcpClient is occupied, we will set isAvailable to false.
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
{
    if (tcpi.LocalEndPoint.Port == port)
    {
        isAvailable = false;
        break;
    }
}

Console.WriteLine("Port is available");

My first question is: is it possible to return to inno setup the true or false value from the port test code? If its false then I need to let the user know.

My second question is: is the port test code correct?

My third question is: am I right in thinking I will then potentially need to open that port in the firewall.

标签: c# inno-setup