vendredi 22 janvier 2016

Creating a string array to call later in C#

Creating a remote installer application in C#. First, I am doing a ping check to make sure the computer is alive before i run the install on it. I am going to implement service checks later.

I need to create a string array that will be based on the ping status being a success (goodPing) and a failure (badPing), so I can use it in another foreach loop where the installs will run.

foreach (string singleComputer in computerArray)
        {
            // Ping all computers
            Ping ping = new Ping();
            PingReply pingresult = ping.Send(singleComputer);

            if (pingresult.Status.ToString() == "Success")
            {
               string[] goodPing = new string[] {"singleComputer"};
            }
            else if (pingresult.Status.ToString() != "Success")
            {
               string[] badPing = new string[] {"singleComputer"};
            }
        }

Basically, I am making sure the computer is pingable, separating the "good" and "bad" computers. computerArray is a string array created from a file.

This is what I want it to look like after I create the arrays:

foreach (string computer in goodPing)
{
     //run installation code here.
}

Obviously, I cannot call goodPing because it is created in an if statement. Please enlighten me on what I am doing wrong.

Aucun commentaire:

Enregistrer un commentaire