/// <summary> /// Writes info data for this folder on disk. /// </summary> public void WriteFolderFileInfo() { string filename = ""; try { filename = FolderInfoFileLocation(); PGFolderInfo f = new PGFolderInfo(); f.Security = this.Security; foreach (PGImage image in this.Images) { PGImageInfo imageinfo = new PGImageInfo(); imageinfo.ImageFileName = image.FileName; imageinfo.ImageFriendlyName = image.FriendlyName; imageinfo.ImageDescription = image.ImageDescr; imageinfo.Comments = image.Comments; f.ImagesInfo.Add(imageinfo); } //serialization System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(PGFolderInfo)); lock (locker) { using (MemoryStream m = new MemoryStream()) { serializer.Serialize(m, f); //to avoid file corruption if serialization fails using (System.IO.FileStream xmlStream = new System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { m.WriteTo(xmlStream); } m.Close(); } } } catch (Exception ex) { throw new Exception("Error writing folder information file on " + filename, ex); } }
/// <summary> /// Reads data from xml cache file. /// </summary> public void ReadFolder() { string filename = ""; Images = GetImagesFromDiskFolder(); NumberNestedImages += NumberImages; if (ExistFolderInfoFile()) { try { lock (locker) { filename = Path.Combine(this.PhysicalPath, cfolderInfo); System.Xml.Serialization.XmlSerializer dxml = new System.Xml.Serialization.XmlSerializer(typeof(PGFolderInfo)); using (System.IO.FileStream xmlStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { PGFolderInfo info = dxml.Deserialize(xmlStream) as PGFolderInfo; this.Order = info.Order; this.SortAction = info.SortAction; this.Security = info.Security != null ? info.Security : new SecurityInfo(); foreach (PGImageInfo imageinfo in info.ImagesInfo) { //search for that image. PGImage image = this.Images.Where(r => r.FileName == imageinfo.ImageFileName).FirstOrDefault(); if (image != null) { image.FriendlyName = string.IsNullOrWhiteSpace(imageinfo.ImageFriendlyName) ? image.FileName.Replace("_", " ") : imageinfo.ImageFriendlyName; image.Comments = imageinfo.Comments; foreach (Comment c in image.Comments) { c.ImageVPath = image.VPath; Comments.Add(c); } image.ImageDescr = (imageinfo.ImageDescription != null) ? imageinfo.ImageDescription : ""; } else { //image referenced in db file not found in file system. //do nothing, next time file is written to disk will be gone } } xmlStream.Close(); } } } catch (Exception ex) { throw new Exception("Error reading folder information file on " + filename, ex); } } if (Configuration.GetConfiguration().ReadBackWardsCompatibilityTxtFiles) { //backwards compatibility. any txt file associated to picture? foreach (PGImage image in this.Images) { string txtFile = image.PhysicalPath.Replace(Path.GetExtension(image.PhysicalPath), ".txt"); if (File.Exists(txtFile)) { using (StreamReader sr = new StreamReader(txtFile)) { image.ImageDescr = sr.ReadToEnd(); } } } } }