[HELP] Save/Load Windows Form C#
I've done a simple program that saves one listbox to a file called fname.txt and the other listbox to lname.txt, but if I start with pressing the load button it loads the both files and write them in the listbox, but if I once pressed load and want to save after that it says fname.txt and lname.txt is currently used by another process so it can't save it.
Any suggestions ?
Load source:
Code:
private void load_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
OpenFileDialog Open = new OpenFileDialog();
Open.FileName = "./fname.txt";
try
{
StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
while (Import.Peek() >= 0)
listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
this.listBox1.Items.Clear();
OpenFileDialog Open2 = new OpenFileDialog();
Open.FileName = "./lname.txt";
try
{
StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
while (Import.Peek() >= 0)
listBox2.Items.Add(Convert.ToString(Import.ReadLine()));
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
}
Save source:
Code:
private void save_Click(object sender, EventArgs e)
{
StreamWriter Write;
SaveFileDialog Open = new SaveFileDialog();
try
{
Open.FileName = "./fname.txt";
Write = new StreamWriter(Open.FileName);
for (int I = 0; I < listBox1.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox1.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
StreamWriter Write2;
SaveFileDialog Open2 = new SaveFileDialog();
try
{
Open.FileName = "./lname.txt";
Write2 = new StreamWriter(Open.FileName);
for (int I = 0; I < listBox2.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(listBox1.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
}
Re: [HELP] Save/Load Windows Form C#
One more thing: when I hit save and then save or load it gives me same error.
Re: [HELP] Save/Load Windows Form C#
Close the file before reopening it.
Re: [HELP] Save/Load Windows Form C#
So I just add Close.Filename("fname.txt"); after the code ?
Re: [HELP] Save/Load Windows Form C#
I found what's wrong.
you can clolse this topic
Re: [HELP] Save/Load Windows Form C#
Please tell us too what was wrong :wink:
Not that it matters, you didn't read the rules so it's an infraction + lock for you anyway.
Want to know why? Read the rules and figure it out :wink:
Re: [HELP] Save/Load Windows Form C#
Quote:
Originally Posted by
baever
I found what's wrong.
you can clolse this topic
FFS can you be any more of a leech? It's good practice everywhere to post the answer to a problem even if you were the one in need of help in the first place. I'm reopening this thread cause I want to see the answer posted. (plus it's always nice to step on Francis' toes :tongue:)
Re: [HELP] Save/Load Windows Form C#
Quote:
Originally Posted by
Negata
I'm reopening this thread cause I want to see the answer posted. (plus it's always nice to step on Francis' toes :tongue:)
Ow damn you! He'd better be posting the answer quick or I'll lock this again! :tongue: