using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
namespace User
{
    [Serializable ]
    public class UserInformation
    {
        private string UserName;
        private string UserPassWord;        public UserInformation() { }
        public UserInformation(string name, string password)
        {
            UserName = name;
            UserPassWord = password;
        }        public override string ToString()
        {
            return string.Format("Name:{0},PassWord:{1}", UserName, UserPassWord);
        }
    }    [Serializable]//使之能够序列化
    public class UserCollection
    {
        private ArrayList UserList = new ArrayList();        public UserCollection() { }        public UserInformation  GetUser(int pos)
        {
            return (UserInformation )UserList[pos];
        }        public void AddUser(UserInformation  p)
        {
            UserList.Add(p);
        }        public void ClearUser()
        {
            UserList.Clear();
        }        public int GetCount()
        {
            return UserList.Count;
        }    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using User;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization ;namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string name, password;
            //UserInformation a=new UserInformation ();
            UserCollection b=new UserCollection ();            FileInfo f = new FileInfo(@"D:\Test.dat");
            FileStream fs = f.Create();
            fs.Close();            for (int i = 0; i < 2; i++)
            {
                name = Console.ReadLine();
                password = Console.ReadLine();
                b.AddUser(new UserInformation(name, password));
            }
            int count = b.GetCount();
            Console.WriteLine(count);
            for (int j = 0; j < count; j++)
            {
                Console.WriteLine(b.GetUser(j));
            }
            SaveAsBinaryFormat(b,"Test.dat");
            Console.ReadLine();
        }        static void SaveAsBinaryFormat(object obj, string filename)
        {
            BinaryFormatter binFormat = new BinaryFormatter();            using (Stream fStream = new FileStream(filename,
                FileMode.Create, FileAccess.Write, FileShare.None))
            {
                binFormat.Serialize(fStream, obj);
            }
        }
    }
}
为什么test.dat文件中没有存入任何东西

解决方案 »

  1.   

    MyObject obj = new MyObject(); 
      IFormatter formatter = new BinaryFormatter(); 
      Stream stream = new FileStream("", FileMode.Create, 
      FileAccess.Write, FileShare.None); 
      formatter.Serialize(stream, obj); 
      stream.Close(); 
    List<T> list= new List<T>();
    XmlSerializer ser = new XmlSerializer(list.GetType());  
    ser.Serialize(new FileStream(@"a.xml", FileMode.Create), list); 
      

  2.   


      using (Stream fStream = new FileStream(filename,
      FileMode.Create, FileAccess.Write, FileShare.None))
      {
      binFormat.Serialize(fStream, obj);
      fStream.Flush();
      fStream.Close();
      }
      }
      

  3.   

    FileInfo f = new FileInfo(@"D:\Test.dat");
    SaveAsBinaryFormat(b,"Test.dat");
    看看你先后写的两个文件,后一个传入的文件名是在你的程序启动路径下,不一定是D盘根目录吧。
      

  4.   

            ///   把对象序列化并返回相应的字节   
            public static byte[] SerializeObject(object objectToSerialize)
            {
                if (objectToSerialize == null)
                    return null;
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, objectToSerialize);
                memoryStream.Position = 0;
                byte[] read = new byte[memoryStream.Length];
                memoryStream.Read(read, 0, read.Length);
                memoryStream.Close();
                return read;
            }
      

  5.   

    开始是这个文件
    FileInfo f = new FileInfo(@"D:\Test.dat")过一会儿成了这个文件
    SaveAsBinaryFormat(b,"Test.dat");你确定此文件是彼文件么?