简体   繁体   English

如何在C#中将数据列表视图保存到.txt,以便下次打开表单时可以访问它?

[英]How to save a listview of data to a .txt in C# so it can be accessed the next time the form is open?

How can I save the the data in a listview? 如何将数据保存在列表视图中?

For example if the user has put something into the listview and then closes the application, all that information is then lost. 例如,如果用户将某些内容放入列表视图中,然后关闭应用程序,则所有这些信息都将丢失。 Is there any way of saving the information as soon as they put something into the listView? 将信息放入listView后,有什么方法可以保存信息?

This is the code I use to input the information from a textbox to the listView1: 这是我用来将信息从文本框输入到listView1的代码:

  string[] items = { titletxt.Text, statustxt.Text };
        ListViewItem lvi = new ListViewItem(items);
        listView1.Items.Add(lvi);

UPDATE (29/12/2012) 更新(29/12/2012)

Thanks for all the help! 感谢您的所有帮助! But I can't seem to get it working. 但是我似乎无法正常工作。 I have created a really simple form to try and get it working. 我创建了一个非常简单的表单来尝试使其正常运行。 It has 3 textboxes (NameTxt, AgeTxt, HairColourTxt), a button, and the listview(ListOfPeople). 它具有3个文本框(NameTxt,AgeTxt,HairColourTxt),一个按钮和listview(ListOfPeople)。

Here is the code: 这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace listview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] items = { NameTxt.Text, AgeTxt.Text, HairColourTxt.Text };
            ListViewItem lvi = new ListViewItem(items);
            ListOfPeople.Items.Add(lvi);
        }
    }
}

I have tried to add the code posted by @user574632 & @Joe & @DmitryKvochkin and I have changed bits but still cant get it to work. 我试图添加由@ user574632&@Joe&@DmitryKvochkin发布的代码,但我已更改了一些位,但仍无法使其正常工作。 I'm not sure what I'm doing wrong? 我不确定自己在做什么错? I have tried to add this: 我尝试添加以下内容:

     private void saveListOfPeopleItems(string path, ListView lv)
    {
        var delimeteredListviewData = new List<string>();

        foreach (ListViewItem lvi in lv.Items)
        {
            string delimeteredItems = string.Empty;
            foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
            {
                delimeteredItems += lvsi.Text + "#";
            }
            delimeteredListviewData.Add(delimeteredItems);
        }

        System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
    }

    private void loadListOfPeopleItems(string path, ListView lv)
    {

        foreach (string line in System.IO.File.ReadAllLines(path))
        {
            lv.Items.Add(new ListViewItem(line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)));
        }
    }

You need to create the save logic, there's no automatic method. 您需要创建保存逻辑,没有自动方法。

The easiest way is to use Serialization (look for ISerializable ) to save to a file of your choice (name it whatever you like): You can save entire objects this way. 最简单的方法是使用Serialization (查找ISerializable )将其保存到您选择的文件(随便命名):您可以用这种方式保存整个对象。 Here is a simple tutorial on serialization. 这是有关序列化的简单教程。

The other method is to parse the listview content into strings (keep only what you need), and you save the strings to a text file ( TextReader and TextWriter ). 另一种方法是将listview内容解析为字符串(仅保留您需要的内容),然后将字符串保存到文本文件( TextReaderTextWriter )中。

If you want to save application settings (and not user data), have a look at this link , or this one which might be easier to read. 如果你想保存程序的设置(而不是用户数据),看看这个链接 ,或这一个 ,这可能是更容易阅读。

Finally, if you need to interact with the data you save regularly, or if you have a lot of data to store, use a database (SQL, mySQL, etc). 最后,如果需要与定期保存的数据进行交互,或者要存储大量数据,请使用数据库(SQL,mySQL等)。 The latter method is the longest one to implement. 后一种方法是最长的实现方法。

if you want to save to a text file, you could use the following: 如果要保存到文本文件,可以使用以下命令:

 private void saveListViewItems(string path, ListView lv)
    {
        var delimeteredListviewData = new List<string>();

        foreach (ListViewItem lvi in lv.Items)
        {
            string delimeteredItems = string.Empty;
            foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
            {
                delimeteredItems += lvsi.Text + "#";
            }
            delimeteredListviewData.Add(delimeteredItems);
        }

        System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
    }

    private void loadListViewItems(string path, ListView lv)
    {

        foreach (string line in System.IO.File.ReadAllLines(path))
        {
            lv.Items.Add(new ListViewItem(line.Split(new char[]{'#'}, StringSplitOptions.RemoveEmptyEntries)));
        }
    }

You can use integrated visual studio settings and than bind it to your listview, even in designer. 您可以使用集成的Visual Studio设置,甚至可以将其绑定到列表视图(即使在Designer中)。 After you changed your listview you can save them 更改列表视图后,可以保存它们

我想说的答案是:使用.txt文件也可以创建一个SQL数据库,并将其保存在其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM