1

我想把我的列表保存到一个文本文件中,所以我把它转换成一个数组,现在我想把它写下来。

        public void Save(Group g)
    {
        string[] lines = g.elementsList.ConvertAll(p => p.ToString()).ToArray();
        BinaryFormatter bf = new BinaryFormatter();
        using (Stream file = File.OpenWrite(path)) 
        {
            foreach (string line in lines)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, lines);
                    byte[] ser = ms.ToArray();

                    <--------stuck here :(

                }
            }
        }

我如何从这里继续?还是我应该改变整个方法..

4

1 回答 1

2

BinaryFormatter不写文字;如果您想写文本,请不要使用BinaryFormatter. 同样,您当前lines每次都在编写,而不是line. 但这都是学术性的:所有这些都只是:

File.WriteAllLines(path, lines);

而已; 这就是执行此操作的全部代码。

于 2013-02-07T10:07:05.163 回答