2009-03-13 8 views
0

두 개의 개별 ListView에있는 데이터 컬렉션을 반복하는 프로그램 논리에 문제가 있습니다. 루프를 반복하고 ListView에서 데이터를 추출한 다음 모든 것을 쉼표로 구분 된 텍스트 파일 (CLOSEULDCONFIG.TXT)에 추가합니다.잘못된 데이터가 ListView에서 검색되었습니다.

이 논리를 처음 실행하면 모든 것이 올바르게 작동합니다. 이 논리를 다시 실행하면 ListView에있는 사본 2 부를 얻습니다. 이 로직을 실행할 때마다 이전에 추가 된 ListView 항목의 복사본 수가 1 씩 증가합니다.

ListView에있는 것과 동일한 수의 요소를 추가하려는 경우 바람직하지 않습니다 텍스트 파일. 누구든지이 문제를 일으키는 내 중첩 된 foreach 문을 잘못 판단 할 수 있습니까?

     // HAZMAT PACKAGE ERROR LISTVIEW ITEMS    
         foreach (ListViewItem HazPackErrItems in HazmatPackageErrorListview.Items) 
         { 
          bool first = true; 
          foreach (ListViewItem.ListViewSubItem HazPackErrSub in HazPackErrItems.SubItems) 
          { 
           // removes the first element of each comma delimited string 
           if (first) 
            first = false; 
           else 
            CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig += " " + HazPackErrSub.Text + ","; 
          } 
         } 

         // HAZMAT WEIGHT AND COUNT COLLECTED LISTVIEW ITEMS 
         foreach (ListViewItem HazWeightAndCountItems in HazmatWeightAndCountListview.Items) 
         { 
          bool first = true; 
          foreach (ListViewItem.ListViewSubItem HazWeightAndCountSub in HazWeightAndCountItems.SubItems) 
          { 
           // removes the first element of each comma delimited string 
           if (first) 
            first = false; 
           else 
            CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig += " " + HazWeightAndCountSub.Text + ","; 
          } 
         } 

         using (System.IO.StreamWriter sw = new System.IO.StreamWriter("CLOSEULDCONFIG.TXT", true)) 
         { 
          if (!AlreadyExists) 
          { 
           sw.WriteLine(PresetNameConfig + 
           CloseULDSubmitLogDataRequestCloseULDConfig + 
           CloseULDSubmitLogDataResponseCloseULDConfig + 
           CloseULDSubmitLogDataResponseHazpackerrCloseULDConfig + 
           CloseULDSubmitLogDataResponseHazWeightAndCountCloseULDConfig + 
           CloseULDDateTimeConfig); 
          } 
         } 

답변

1

내가 실수하지 않은 경우 덮어 쓰기 대신 첨부 할 파일을 여는 중입니다. 파일 자체를 검사하여 데이터가 거기에 복제되는지 확인 했습니까?

관련 문제