0

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;
            filestream2.Close();

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

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by wingers1290
Featured Replies
  • 1
    ddanbe 2,525   7 Years Ago

    [CODE=c#]OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { MyFileNameToUseWhereIWant = dlg.FileName; // perhaps other code here }[/CODE] Is all there is to it. Happy coding! Read More

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

Is all there is to it. Happy coding!

Comments
Brilliant
0

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?

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.