Hi, I am using the following code to open a file "Zl2WIN".

FileStream filestream2 = new FileStream("ZL2WIN", FileMode.Open);
            BinaryFormatter bf2 = new BinaryFormatter();

            Person JohnDoe = new Person();
            JohnDoe = (Person)bf2.Deserialize(filestream2);
            textBox7.Text = JohnDoe.CallSign;
            textBox8.Text = JohnDoe.Freq;
            textBox9.Text = JohnDoe.Time;
            textBox10.Text = JohnDoe.fName;
            textBox11.Text = JohnDoe.lName;
            textBox12.Text = JohnDoe.Country;

My question is how can I open up an open file dialog instead of defining a name in the code. Thanks.

OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
    MyFileNameToUseWhereIWant = dlg.FileName;
    // perhaps other code here

Is all there is to it. Happy coding!


Thanks for the reply, i'm still having trouble though.

MyFileNameToUseWhereIWant = dlg.FileName;

Big question mark, this won't compile not sure what you mean because i want to be able to select a file name.

also when it come to this line

JohnDoe = (Person)bf2.Deserialize(filestream2);

what do i enter instead of filestream2 to load the file i specified?

Sorry, my fault I should have said that MyFileNameToUseWhereIWant is a string variable defined elsewhere.
Put this as variable in FileStream filestream2 = new FileStream("ZL2WIN", FileMode.Open); there where you use "ZL2WIN"
Thought it was obvious:-O and of course you will give a different name to this filename variable. If the dlg variable is still in scope you can also directly use dlg.FileName.

Brilliant thanks, sorry I wasn't thinking straight there must of missed the obvious.

This question has already been answered. Start a new discussion instead.