Click here to Skip to main content
15,127,226 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,I want to record separate files in the listview elements.

Sample:
streamWriter sw = new streamWriter("data1.txt");
for(int i = 0; i <= listview1.items[0].subitems[0])
{
  sw.writeLine(listview1.items[0].subitems[0][i]);
}

streamWriter sw2 = new streamWriter("data2.txt");
for(int i = 0; i <= listview1.items[0].subitems[1])
{
  sw2.writeLine(listview1.items[0].subitems[1][i]);
}


[edit]Code block added - OriginalGriff[/edit]
[edit]Respaced - OriginalGriff[/edit]
Posted
Updated 26-Jan-11 4:49am
v3

I can see a lot of problems; after some guesswork I think you tried to do something like this:

C#
using System.IO;
using System.Window.Forms;

//..

ListView listView = new ListView();

//..

int fileIndex = 0;
foreach(ListViewItem listViewItem in listView.Items) {
    using (StreamWriter writer =
        new StreamWriter(string.Format("{0}{1}.txt", listView.Name, fileIndex))) {
        writer.WriteLine(listViewItem.Text);
        foreach (ListViewItem subitem in listViewItem.SubItems)
            writer.WriteLine(subitem.Text);
   } //using; writer is disposed here -- important
   fileIndex++;
} //list item loop


This fragment writes one "line" of your tree in a separate file (ask yourself -- why?!), lines in a file represent sub-items by the values of their Text property; first line is the file represent an item, not a sub-item. I combined file names out of the Name of listView and integer index of the list view item fileIndex.

You did not try to dispose StreamWriter, so your files could be not flushed to disk; in the code above it is done via "using" construct (Dispose called automatically when block is finished, even on exception); addressing if items was wrong (there is no "[][]" anywhere). Also, WriteLine require string, not ListViewItem. Needless to say, the "code" in Question would not even compile.

Even type names are not typed properly -- how you explain it?
   
v3
Look at using a foreach loop instead of a for loop: That way you don't need to know how many items / subitems there are.

ListBox.Items[index] returns a object - in order to use any subitems, you will need to cast it to the appropriate class first. Then you can use the foreach loop to iterate thorough them and save as text. Without knowing more about your ListBox content, there isn't a lot more I can suggest.
   
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 10:43am
   
PO commented:

Hello,Can you give an example?
Sergey Alexandrovich Kryukov 26-Jan-11 10:44am
   
I gave you an example, please see my Answer.
Sergey Alexandrovich Kryukov 26-Jan-11 10:46am
   
I moved your "Answer" in comment.
Please don't post non-answers as answers: 1) nobody will receive notification; 2) your answer will be removed, because it is not an answer. Use "Add Comment" instead.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900