开发者

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


Your "Ore" class must be Serializable

[Serializable] Class Ore
{
 . 
 .
 .
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜