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.

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");
			}
				
					
		}

I also tried:
output.Write(" {0}, {1}, {2}, {3}, {4}",fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);

Didnt work either. :(

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.

Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.

Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.

Thank you that made sense. However, I changed it to:

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");
			}
				
					
		}

But guess what? The file is still blank! LOL. Any other ideas that I can try? I know the info is stored in the array I can view it add new ones and delete them. Just don't know why they wont write.

Hello, C#Newbie. Try to add Flush before closing your StreamWriter.

in your case it's:

output.flush();

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()

Oh My God. Just out of curiosity I clicked on the savemenu Item again to see if it goes to that event and guess what. It opened another event. That is so weired since I did the first savemenu Item_click event the same way. Anyhow...It works now....whooo wheeee. Thanks to all of you!

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.