c# - Read a file into a list by a class does not working -


i wrote android app xamarin in c#, , made application class manage file data wich want load in list.

the app class:

namespace soroksar_sc_stat { [application] public class getdataclass : android.app.application {      public getdataclass (){}      private string filename = path.combine(system.environment.getfolderpath(system.environment.specialfolder.applicationdata),"playerdata.txt");     private directoryinfo di = directory.createdirectory(system.environment.getfolderpath(system.environment.specialfolder.applicationdata));     private filestream myfile = new filestream(path.combine(system.environment.getfolderpath(system.environment.specialfolder.applicationdata),"playerdata.txt"), filemode.create);      public list<string> getlist()     {         streamreader myreader = new streamreader(this.myfile);         string line;         list<string> list = new list<string>();         while((line = myreader.readline()) != null)         {             list.add(line);         }          myreader.close ();         list.add ("blabla");         return list;     }      public void setnewdata (string playername, datetime borndate)     {          string newline = playername + " " + borndate.tostring () + " 0 0 0 0";         streamwriter mywriter = new streamwriter(this.myfile);         mywriter.writeline(newline);         mywriter.close();      }     } } 

the activity should show list

namespace soroksar_sc_stat { [activity (label = "dataactivity")]          public class dataactivity : activity {     protected override void oncreate (bundle bundle)     {         base.oncreate (bundle);          setcontentview (resource.layout.datalayout);          button addbutton = findviewbyid<button> (resource.id.databutton);         listview lista = findviewbyid<listview> (resource.id.listview1);          getdataclass datalist = new getdataclass();           list<string> list = datalist.getlist();          addbutton.click += delegate          {             intent intent = new intent(this, typeof(adddataactivity));             this.startactivity(intent);         };          }      } } 

the first 1 class reading. empty file, troubleshooting added string @ end of reading. if start emulator in second activity (which code second one) list not have item. don't know problem is, if me appreciate it.

as mentioned in comment, think because aren't closing streamreader correctly, may closing before ever writing file. solution 1 of following

public void setnewdata (string playername, datetime borndate) {      string newline = playername + " " + borndate.tostring () + " 0 0 0 0";     using(streamwriter mywriter = new streamwriter(this.myfile))         mywriter.writeline(newline); } 
public void setnewdata (string playername, datetime borndate) {      string newline = playername + " " + borndate.tostring () + " 0 0 0 0";     streamwriter mywriter = new streamwriter(this.myfile);     mywriter.writeline(newline);     mywriter.flush();     mywriter.close(); } 

please note: first way preferred way makes sure cleanup done correctly. should use using block reading too

using(streamreader myreader = new streamreader(this.myfile)) {     string line;     list<string> list = new list<string>();     while((line = myreader.readline()) != null)     {         list.add(line);     } } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -