visual studio - Im trying to make a write to text file in c# -
i have code doesn't seem doing bit stuck
const string spath = "movieadd.txt"; system.io.streamwriter savefile = new system.io.streamwriter(spath); if (listbox1.selecteditems.count ==1) { foreach (var item in listbox1.selecteditems) { savefile.writeline(item); } savefile.close(); }
the code writes lines when 1 entry in list selected. i'm unsure whether want, considering you're trying write 1 line per selected item. may want rewrite code following, allows more 1 row selected. also, file closed in case in following code.
const string spath = "movieadd.txt"; if (listbox1.selecteditems.count >= 1) { using (system.io.streamwriter savefile = new system.io.streamwriter(spath)) { foreach (var item in listbox1.selecteditems) { savefile.writeline(item); } } }
another issue may don't have explicit path in spath
variable. may cause problems, depending on current working directory, may different directory executable resides in! safer add directory explicitly, this:
const string spath = @"c:\temp\movieadd.txt"; if (listbox1.selecteditems.count >= 1) { using (system.io.streamwriter savefile = new system.io.streamwriter(spath)) { foreach (var item in listbox1.selecteditems) { savefile.writeline(item); } } }
Comments
Post a Comment