| | |
Writing ArrayList to textFile?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
Hi. I am working on my Final for my Intro to C# class so yes I am new and my code is pretty unprofessional probably. We are required to use a array or a class to read addresses from a text file and display them in the GUI. We should be able to add new addresses and delete address and sort the arraylist by Name. Ok Everything works except...the reading and writing to the text file and the sorting. I tried the following to write to the text file which I have in the bin/debug folder of the program. Also I am using Visual Studio 2003 .Net Framework1.1 just in case that helps. ok here is the code I don't get any errors but the file is still blank when I open it.
I also tried:
output.Write(" {0}, {1}, {2}, {3}, {4}",fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);
Didnt work either.
C# Syntax (Toggle Plain Text)
private void menuSave_Click(object sender, System.EventArgs e) { // if the file exists if (File.Exists("AddressList.txt")) { try { // creates a new StreamWriter StreamWriter output = new StreamWriter(Application.StartupPath + "\\AddressList.txt"); // writes each friend to the text file foreach(Friend fr in friendArray) { output.Write(fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip); } //closes the file output.Close(); } catch(System.IO.IOException exc) { MessageBox.Show(exc.Message); } } else { MessageBox.Show("File Not Found"); } }
output.Write(" {0}, {1}, {2}, {3}, {4}",fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);
Didnt work either.
•
•
Join Date: Jun 2008
Posts: 58
Reputation:
Solved Threads: 9
Just a quick glance I would play around with this line:
output.Write(fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);
You have to convert the arraylist element into a string or something before writing it to the file. Just make sure it's in a format that you can read back later.
output.Write(fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);
You have to convert the arraylist element into a string or something before writing it to the file. Just make sure it's in a format that you can read back later.
Visual C# Kicks - Free C# code resources and articles.
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.
C# Syntax (Toggle Plain Text)
private void menuSave_Click(object sender, System.EventArgs e) { // if the file exists if (File.Exists("AddressList.txt")) { try { // creates a new StreamWriter ***************************************** // ******************************************************************** // ITS NOT WRITING TO THE FILE. StreamWriter output = new StreamWriter(Application.StartupPath + "\\AddressList.txt"); // writes each friend to the text file foreach(Friend fr in friendArray) { //output.WriteLine("{0}, {1}, {2}, {3}, {4}", fr.friendName.ToString(), fr.friendStreet.ToString(), fr.friendCity.ToString(), fr.friendState.ToString(), fr.friendZip.ToString()); output.WriteLine(fr.friendName.ToString()); output.WriteLine(fr.friendStreet.ToString()); output.WriteLine(fr.friendState.ToString()); output.WriteLine(fr.friendState.ToString()); output.WriteLine(fr.friendZip.ToString()); } //closes the file output.Close(); } catch(System.IO.IOException exc) { MessageBox.Show(exc.Message); } } else { MessageBox.Show("File Not Found"); } }
Hello, C#Newbie. Try to add Flush before closing your StreamWriter.
in your case it's:
in your case it's:
c# Syntax (Toggle Plain Text)
output.flush();
Last edited by Antenka; Feb 12th, 2009 at 11:49 am.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
You are writing to "\\AddressList.txt" instead of "AddressList.txt".
Use Write, use WriteLine if you want to start a new line.
Use the using keyword. That way you don't have to bother to close the stream.
If friendName is already a string you don't have to do friendName.ToString()
Use Write, use WriteLine if you want to start a new line.
Use the using keyword. That way you don't have to bother to close the stream.
If friendName is already a string you don't have to do friendName.ToString()
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
I am Gisha.I am a C# platform programmer.I just want to know the basic steps to create a simple c# application. pls sent me the queries for creation of simple c# application...........
Thank"U"
Regards
(Gisha.K)
and see if that helps. You can download Visual Studio C# 2008 Express Edition free:
http://www.microsoft.com/eXPress/download/
This should get you started.
![]() |
Other Threads in the C# Forum
- Previous Thread: problems compiling first Windows application
- Next Thread: Palindrome test function
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# check checkbox client color combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file files firefox form format forms function gdi+ httpwebrequest image index input install java label libraries list listbox listener mandelbrot math mouseclick mysql mysql.data.client operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resourcefile richtextbox serialization server sleep socket sql statistics stream string table tcp tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf xml






