2

i have a button in my form that is supposed to take a list and save it to binary file on click. i compile and run the program enter the valued in the text box and click the save button. i look in the project directory and there is no new file. did i code it wrong or miss something?

private void button1_Click(object sender, EventArgs e)
{
    List<ore> oreData = new List<ore>();
    oreData.Add(b1);
    oreData.Add(b2);

    FileStream fs = new FileStream("ore.dat", FileMode.Create);
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, oreData);
    fs.Close();
}
4
  • 3
    Did you look under your bin directory (Debug or Release, depending on which build you're running)? Commented Jun 26, 2011 at 23:33
  • 1
    OMG i cant believe it was that simple.... well now on to getting it to read that file :) thank you very much Commented Jun 26, 2011 at 23:35
  • 1
    Also, you may want to append Application.StartupPath as the prefix for file names. Commented Jun 26, 2011 at 23:40
  • Aside: Personally, I try to discourage people from using BinaryFormatter as a persistence format. It has some jagged edges that can really hurt you as you evolve your application. There are binary serializers that are far more suitable, IMO. Commented Jun 27, 2011 at 5:37

2 Answers 2

2

Your "Ore" class must be Serializable

[Serializable] Class Ore
{
 . 
 .
 .
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would the Ore class have the oreData as one of its elements?
0

If you're on Windows Vista or later and you are not explicitly launching your program with admin privileges then I bet it is being written to a shadow directory under the covers as you are not allowed to write to anything in Program Files. Here is some more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.