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:
Save source:
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;
}
}


