using System.IO; using System.Xml.Serialization; public List<Users> userlist = new List<Users>(); public List<Users> UsersList { get { return userlist; } } public void SaveInformation() { XmlSerializer serializer = new XmlSerializer(typeof(List<Users>)); string filePath = @"serializeUsers.xml"; FileStream fs = File.Create(filePath); serializer.Serialize(fs,UsersList); fs.Close(); } public List<Users> ReadInformation() { XmlSerializer serializer = new XmlSerializer(typeof(List<Users>)); string filePath = @"serializeUsers.xml"; if (!File.Exists(filePath)) { *************************** } FileStream fs = File.OpenRead(filePath); List<Users> lu = (List<Users>)serializer.Deserialize(fs); fs.Close(); return lu; }
开始时用过Directory.Exists(filePath)和Directory.CrreateDirectory(filePath)来尝试,发现只是创建了名为“serializeUsers.xml”的文件夹。
但因为没注意这个文件夹,所以在换为File方法时,总出现错误:
Access to the path ‘*************serializeUsers.xml‘ is denied.
当删除目录下的“serializeUsers.xml”的文件夹时,运行成功。
时间: 2024-10-14 06:32:14