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文件中没有存入任何东西
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文件中没有存入任何东西
解决方案 »
- 我很痛苦,高手来帮我看看怎么写才能实现功能
- C# 用richtextbox控件 运行时“未将对象引用设置到对象的实例”
- 打印问题
- 高手们,请教个高难度问题.放大图片的功能
- winform中如何传值?(就象在webform使用session一样,保存某个文本框的值)
- 多线程问题,估计比较简单
- 呵呵,今天又来了一个问题,不知道如何解决更好!
- 如何将table表导出到excel表中的特定列删除。
- 关于读取文本数据整理再生成
- 如果在C#或VB.NET中用到Office组件,如果程序安装到其有没有装MS Office的电脑能否正常执行,如果不能执行..........
- .Net MySQLHelper问题
- c#怎么移动文件
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);
using (Stream fStream = new FileStream(filename,
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, obj);
fStream.Flush();
fStream.Close();
}
}
SaveAsBinaryFormat(b,"Test.dat");
看看你先后写的两个文件,后一个传入的文件名是在你的程序启动路径下,不一定是D盘根目录吧。
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;
}
FileInfo f = new FileInfo(@"D:\Test.dat")过一会儿成了这个文件
SaveAsBinaryFormat(b,"Test.dat");你确定此文件是彼文件么?