大婶们啊,帮帮我把,我想用TXT文件存储我的数据。
有一个类,它有若干个属性,我想要每次实例化的数据,存到TXT文件中。下次启动的时候,也可以从TXT文件里读取这个对象的信息

解决方案 »

  1.   

    网上搜一下吧,一大把,现在的xml很好用,推荐
      

  2.   

    数据结构。。自己定义比如一行一个属性, 为什么不用xml呢?
      

  3.   

    用txt好像会比较麻烦,你还是转到xml吧
      

  4.   


            public static void WriteTxt(string filePathName,bool append, List<String[]> ls)
            {
                StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
                foreach(String[] strArr in ls)
                {
                    fileWriter.WriteLine(String.Join (“,",strArr) );//换成你实际的分隔符
                }
                fileWriter.Flush();
                fileWriter.Close();
               
            }
            public static List<String[]> ReadTxt(string filePathName)
            {
                List<String[]> ls = new List<String[]>();
                StreamReader fileReader=new   StreamReader(filePathName); 
                string strLine="";
                while (strLine != null)
                {
                    strLine = fileReader.ReadLine();
                    if (strLine != null && strLine.Length>0)
                    {
                        ls.Add(strLine.Split(','));   //换成你实际的分隔符
                        //Debug.WriteLine(strLine);
                    }
                }
                fileReader.Close();
                return ls;
            }
      

  5.   

    大婶们啊,帮帮我把,我想用TXT文件存储我的数据。
      

  6.   


     if (File.Exists("../../test.txt"))
                {
                    if (!this.txtTest.AutoCompleteCustomSource.Contains(txtTest.Text))//判断记录是否存在
                    {
                        StreamWriter sw = new StreamWriter("../../test.txt", false);//true参数不可少,否则会覆盖以前存入的记录
                        sw.WriteLine(this.txtTest.Text.Trim());//存入记录
                        sw.Close();
                        if (!this.txtTest.AutoCompleteCustomSource.Contains(this.txtTest.Text))
                        {
                            this.txtTest.AutoCompleteCustomSource.Add(this.txtTest.Text);
                        }
                    }
                    this.DialogResult = DialogResult.OK;
                } 
      

  7.   

    txtTest是文本框 你可以把你的数据转换成string类型  循环  写入记事本 
      

  8.   

    1。程序启动时:new出这个类的对象,
    2。写一个方法读取txt中的信息到array数组中,
    3。给new出来的对象属性赋值,这里要注意:你给对象属性赋值时,
    每个属性的值要与数组中的值对应,
    4。下次你就可以预先给这个类指定值了。代码很简单,你自己行的。
      

  9.   

     5。你保存数据时 也要以这个指定顺序写入txt文件。