C#保存Listview子项为txt? [英] C# Save Listview Subitems a txt?

查看:42
本文介绍了C#保存Listview子项为txt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在listview元素中记录单独的文件.

样本:

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]已添加代码块-OriginalGriff [/edit]
[edit]重新排列-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]
[edit]Respaced - OriginalGriff[/edit]

推荐答案

我看到了很多问题;经过一番猜测之后,我认为您尝试执行以下操作:

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

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



该片段在单独的文件中写树的行"(问自己-为什么?!),文件中的行通过其Text属性的值表示子项.第一行是文件,代表一个项目,而不是一个子项目.我合并了listViewName中的文件名和列表视图项fileIndex的整数索引.
您没有尝试处置StreamWriter,因此您的文件可能不会刷新到磁盘.在上面的代码中,它是通过"using"构造完成的(Dispose在块完成时自动调用,即使在异常情况下也是如此);解决项目是否错误(在任何地方都没有"[] []").另外,WriteLine需要字符串,而不是ListViewItem.不用说,Question中的代码"甚至都不会编译.

甚至类型名称也不能正确键入-您如何解释?



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?


看看使用foreach 循环而不是for循环:这样,您就不需要知道有多少个项目/子项目.

ListBox.Items [index]返回object-为了使用任何子项,您需要首先将其强制转换为适当的类.然后,您可以使用foreach循环遍历它们并另存为文本.在不了解您的ListBox内容的情况下,我没有太多建议.
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.


这篇关于C#保存Listview子项为txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆