Okay, I've made a save file dialog, and I've been reading but I personally haven't found much useful information that I understand in creating a save button. I have this as a SaveAs. I'm guessing the save button event is probably very very close to this, but with or without a few things.I get lost in trying to get it to Save over the file I'm working on. All I'm trying to do is remove the save file dialogue box in my save menu button.

` SaveFileDialog fileChooser = new SaveFileDialog();

        fileChooser.Title = "Choose Save Location";
        fileChooser.Filter = "Text Files (*.txt)|*.txt";

        //open dialog, get result
        DialogResult result = fileChooser.ShowDialog();

        //did they click cancel?
        if (result == DialogResult.Cancel)
        {
            return;
        }

        //get the file name from the dialog
        string strFileName = fileChooser.FileName;

        try
        {
            //save via filestream
            //open the new file for write access


            if (bCheck == true)
            {
                output = new FileStream(strFileName,
                    FileMode.OpenOrCreate, FileAccess.Write);
                bCheck = false;
            }
            else
            {
                output = new FileStream(strFileName,
                  FileMode.Append, FileAccess.Write);

            }
            fileWriter = new StreamWriter(output);


            foreach (Employees employees in employee)
            {
                fileWriter.WriteLine(employees.ToCSVString());

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //close resources
            fileWriter.Close();
            output.Close();
        }`

One day you'll hear about how I broke the internet. Not on purpose, but seriously on accident.

i think...just maybe...i have an idea of how to do this. i'll run this by, and if you see this, let me know if i'm at least on the right track. okay, i assume if i'm understanding my own code correctly(i never do) that strFileName is needing to be passed to my Savebutton click event to do this, say like...this:

Path.GetFileName(strFileName)

I've tried this but it's not quite working yet.

Okay...here's my save button event, i took some stuff out thinking hey, i don't need this or that, let's just do this. Now I'm getting an Empty Path Name is Not Legal error. Lol. I think I'm making progress, I just hope I'm not going backwards with this.

           //get the file name from the dialog
           Path.GetFileName(strFileName);
           try
           {
               //save via filestream
               //open the new file for write access


               if (bCheck == true)
               {
                   output = new FileStream(strFileName,
                       FileMode.OpenOrCreate, FileAccess.Write);
                   bCheck = false;
               }
               else
               {
                   output = new FileStream(strFileName,
                     FileMode.Create, FileAccess.Write);

               }
               fileWriter = new StreamWriter(strFileName);


               foreach (Employees employees in employee)
               {
                   fileWriter.WriteLine(employees.ToCSVString());

               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           finally
           {
               //close resources
               fileWriter.Close();
               output.Close();
           }

       }

shhh, don't say anything yet, I think I almost have it. I'm playing around with MessageBox.Show to at least show me if I've got it yet or not lol....

Okay. I think I GOT IT!!! OMG! Now I just need to do some other stuff. I have a few bruises and scrapes, but I think I'll be okay.

If someone else comes wandering in wondering what I did....I made strFileName static and put it up out of my voids. It wasn't working for me the first few times because I had something else going on with it.

oh, but strFileName is still set in each void, it's just declared out of the voids.

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.