1

我正在尝试从包含 30 多个项目的列表中返回所有数据。出于某种原因,我的代码只给了我列表的最后一项。我确信这是一个简单的解决方法,但如果有人可以帮助我,那就太好了。这是代码。

List<string> propnumList = new List<string>();
foreach (DataRow drRow in ds7.Tables[0].Rows)
{
    for (int i = 0; i < ds7.Tables[0].Columns.Count; i++)
    {
        propnumList.Add(drRow[i].ToString());
    }
}

using (StreamWriter sw = new StreamWriter("propnumList.txt"))
{
    foreach (string s in propnumList)
    {
        sw.WriteLine(s);
    }
}

string tempProp = "";
foreach (string x in propnumList)
{
    if (x.Length < 30)
    {
        x.Equals(null);
    }
    else
    {
        tempProp = x.Substring(31);
        using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
        {
            write.WriteLine(tempProp); WANT TO RETURN MORE THAN JUST LAST ITEM
        }
    }
}
4

1 回答 1

15

您正在覆盖每次迭代的输出文件。将文件打开放在 foreach 之外。

using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
   foreach (string x in propnumList)
   {
     ....
   }
}
于 2013-08-14T19:22:50.383 回答