[HELP] Save/Load Windows Form C#

Newbie Spellweaver
Joined
Sep 19, 2005
Messages
39
Reaction score
0
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;
            }
        }
 
One more thing: when I hit save and then save or load it gives me same error.
 
So I just add Close.Filename("fname.txt"); after the code ?
 
I found what's wrong.
you can clolse this topic
 
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:)
 
Back