doc
doc

Reputation: 123

C# save list to binary file

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();
}

Upvotes: 2

Views: 8172

Answers (2)

Arash Gh
Arash Gh

Reputation: 21

Your "Ore" class must be Serializable

[Serializable] Class Ore
{
 . 
 .
 .
}

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124790

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.

Upvotes: 0

Related Questions