C# 创建字符串数组以便稍后在C中调用#

C# 创建字符串数组以便稍后在C中调用#,c#,.net,arrays,if-statement,installation,C#,.net,Arrays,If Statement,Installation,在C#中创建远程安装程序应用程序。首先,我正在执行ping检查,以确保计算机在运行安装之前处于活动状态。稍后我将实施服务检查 我需要创建一个基于ping状态的字符串数组,ping状态为成功(goodPing)和失败(badPing),这样我就可以在安装运行的另一个foreach循环中使用它 foreach (string singleComputer in computerArray) { // Ping all computers

在C#中创建远程安装程序应用程序。首先,我正在执行ping检查,以确保计算机在运行安装之前处于活动状态。稍后我将实施服务检查

我需要创建一个基于ping状态的字符串数组,ping状态为成功(goodPing)和失败(badPing),这样我就可以在安装运行的另一个foreach循环中使用它

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"};
            }
        }
基本上,我要确保计算机是可ping的,将“好”和“坏”计算机分开。computerArray是从文件创建的字符串数组

这就是我希望在创建阵列后它的外观:

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

显然,我不能调用goodPing,因为它是在if语句中创建的。请告诉我我做错了什么。

在运行循环之前,您需要创建一次数组。数组的问题是没有简单的方法来更改数组的大小,而且您也不知道每个数组中会有多少条记录。因此,我建议使用
List
而不是数组:

List<string> goodPing = new List<string>();
List<string> badPing = new List<string>();
Ping ping = new Ping();

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

    if (pingresult.Status.ToString() == "Success")
    {
         goodPing.Add(singleComputer);
    }
    else if (pingresult.Status.ToString() != "Success")
    {
         badPing.Add(singleComputer);
    }
}

在运行循环之前,需要创建一次数组。数组的问题是没有简单的方法来更改数组的大小,而且您也不知道每个数组中会有多少条记录。因此,我建议使用
List
而不是数组:

List<string> goodPing = new List<string>();
List<string> badPing = new List<string>();
Ping ping = new Ping();

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

    if (pingresult.Status.ToString() == "Success")
    {
         goodPing.Add(singleComputer);
    }
    else if (pingresult.Status.ToString() != "Success")
    {
         badPing.Add(singleComputer);
    }
}

您需要使用泛型列表而不是数组,并且需要在for循环之前声明它们,以便在for循环之后可以访问它们

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

            if (pingresult.Status.ToString() == "Success")
            {
               goodPings.Add(singleComputer); // and don't forget to use the variable singleComputer here, not the literal string "singleComputer".
            }
            else if (pingresult.Status.ToString() != "Success")
            {
               badPings.Add(singleComputer);
            }
        }
List goodPings=new List();
列表添加=新列表();
foreach(计算机阵列中的字符串单计算机)
{
//Ping所有计算机
Ping Ping=新Ping();
PingReply pingresult=ping.Send(单机);
if(pingresult.Status.ToString()=“成功”)
{
goodPings.Add(singleComputer);//不要忘记在这里使用变量singleComputer,而不是文字字符串“singleComputer”。
}
else if(pingresult.Status.ToString()!=“成功”)
{
添加(单机);
}
}

您需要使用泛型列表而不是数组,并且需要在for循环之前声明它们,以便在for循环之后可以访问它们

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

            if (pingresult.Status.ToString() == "Success")
            {
               goodPings.Add(singleComputer); // and don't forget to use the variable singleComputer here, not the literal string "singleComputer".
            }
            else if (pingresult.Status.ToString() != "Success")
            {
               badPings.Add(singleComputer);
            }
        }
List goodPings=new List();
列表添加=新列表();
foreach(计算机阵列中的字符串单计算机)
{
//Ping所有计算机
Ping Ping=新Ping();
PingReply pingresult=ping.Send(单机);
if(pingresult.Status.ToString()=“成功”)
{
goodPings.Add(singleComputer);//不要忘记在这里使用变量singleComputer,而不是文字字符串“singleComputer”。
}
else if(pingresult.Status.ToString()!=“成功”)
{
添加(单机);
}
}

使用这种方法有什么缺点吗?“你知道吗?我很感激你的快速反应。”朱利安约翰逊不确定我是否理解关于缺点的问题。你的意思是在循环之前创建变量的缺点吗?我很抱歉。我现在明白列表的用法了。如果我使用数组,在运行脚本或对其应用计数之前,我不知道我有多少条记录。谢谢你的帮助。使用这种方法有什么坏处吗?“你知道吗?我很感激你的快速反应。”朱利安约翰逊不确定我是否理解关于缺点的问题。你的意思是在循环之前创建变量的缺点吗?我很抱歉。我现在明白列表的用法了。如果我使用数组,在运行脚本或对其应用计数之前,我不知道我有多少条记录。谢谢你的帮助。