c# Help with opening a file.
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.
wingers1290
Junior Poster in Training
89 posts since May 2009
Reputation Points: 12
Solved Threads: 0
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
MyFileNameToUseWhereIWant = dlg.FileName;
// perhaps other code here
}
Is all there is to it. Happy coding!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
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?
wingers1290
Junior Poster in Training
89 posts since May 2009
Reputation Points: 12
Solved Threads: 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.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Brilliant thanks, sorry I wasn't thinking straight there must of missed the obvious.
Cheers.
wingers1290
Junior Poster in Training
89 posts since May 2009
Reputation Points: 12
Solved Threads: 0