Hi, thanks for all the help with my code so far, I just have one more nagging chink in the chain.
This is my code for saving my file which works as expected.

Person Bob = new Person();
                Bob.CallSign = textBox1.Text;
                Bob.Freq = textBox2.Text;
                Bob.Time = textBox3.Text;
                Bob.fName = textBox4.Text;
                Bob.lName = textBox5.Text;
                Bob.Country = textBox6.Text;



                Stream filestream = new FileStream((textBox1.Text), FileMode.Create, FileAccess.Write, FileShare.None);

                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(filestream, Bob);
                filestream.Close();

I would like to be able to change the default save directory by bringing up a file dialog but without changing the name which is currently textbox one.

My trouble is the save location is where i've put textbox1 so i'm stuck with a problem.

I also forgot to mention I can't seem to be able to add an extension on because of the textbox1.

Just as there is an OpenFileDialog class there is a SaveFileDialog one.
Read about it here

I can easily bring up that dialog to save the file to where i want using.

Stream filestream = new FileStream((textBox1.Text), FileMode.Create, FileAccess.Write, FileShare.None);
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((filestream = saveFileDialog1.OpenFile()) != null)
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(filestream, Bob);
                    filestream.Close();
                    filestream.Close();
                }
            }

However i want to have a seperate button that allows me to select a default save location, so pressing the save button will not bring up a dialog it will just save to the default save location which is currently the place where the app is located.

I figured out how to change the save location

Stream filestream = new FileStream(dfl+(textBox1.Text)+".dat", FileMode.Create, FileAccess.Write, FileShare.None);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(filestream, Bob);
                filestream.Close();
            
                MessageBox.Show("Contact Saved");
                Erase();

I just put a string variable infront of the textbox1 file name, now i need to assign the variable but the savefiledialog dosent have a .Location that i can store as the variable so i'm lost for ideas.

I'm now using this to get a string of the location

using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    dfl = dlg.SelectedPath;
                }
            }

which is used in this

#
Stream filestream = new FileStream(dfl+(textBox1.Text)+".dat", FileMode.Create, FileAccess.Write, FileShare.None);
#
BinaryFormatter bf = new BinaryFormatter();
#
bf.Serialize(filestream, Bob);
#
filestream.Close();

However now it doesnt seem to be saving anywhere at all.

Try this code, then look at your output window:

#
string fName = dfl+(textBox1.Text)+".dat";
Console.WriteLine(fName); // you can also break at this line to evaluate fName 
Stream filestream = new FileStream(fName, FileMode.Create, FileAccess.Write, FileShare.None);
#
BinaryFormatter bf = new BinaryFormatter();
#
bf.Serialize(filestream, Bob);
#
filestream.Close();

You should also consider using the System.IO namespace to handle combining paths, ie Path.Combine() .

Report back here with what the file path evaluated to at runtime and that should indicate why you cant find the file. It has to be somewhere on your drive. If it was writing to a nonexistant location you would have seen an IOException thrown.

Its not returning anything at all, i'm not sure what exactly is wrong so i'mm attatch a copy of the project.

The variable 'path' is stated as being null beacuse i found when that ='d null then it would save to the same location as the application.

Stream filestream = new FileStream(path+(textBox1.Text)+".dat", FileMode.Create, FileAccess.Write, FileShare.None);

when i made the fname variable as

fName = path+(textbox1.text)+"dat";

And used that i got nothing at all when i displayed it in a textbox.

Help would really be appreciated as i am well and truly stuck.

I've figured out the problem and it seems logical but slightly weriod.
this is the finished product

#
Stream filestream = new FileStream(dfl+"\\"+(textBox1.Text)+".dat", FileMode.Create, FileAccess.Write, FileShare.None);
#
BinaryFormatter bf = new BinaryFormatter();
#
bf.Serialize(filestream, Bob);
#
filestream.Close();

all i had to do was to add "\\" after the path. when i returned 'path' i realized it had no \ after it so if i saved to the desktop it would actually save to Desktop+name not desktop\name.

Thanks for all your help!

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.