public void TestWriteTwoSections() { String STREAM_NAME = "PropertySetStream"; String SECTION1 = "Section 1"; String SECTION2 = "Section 2"; FileStream file = _samples.GetFile(POI_FS); //filename.deleteOnExit(); FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); ps.ClearSections(); ClassID formatID = new ClassID(); formatID.Bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; MutableSection s1 = new MutableSection(); s1.SetFormatID(formatID); s1.SetProperty(2, SECTION1); ps.AddSection(s1); MutableSection s2 = new MutableSection(); s2.SetFormatID(formatID); s2.SetProperty(2, SECTION2); ps.AddSection(s2); poiFs.CreateDocument(ps.GetStream(), STREAM_NAME); poiFs.WriteFileSystem(out1); //out1.Close(); /* Read the POIFS: */ psa = new PropertySet[1]; POIFSReader reader2 = new POIFSReader(); reader2.StreamReaded += new POIFSReaderEventHandler(reader2_StreamReaded); reader2.Read(file); Assert.IsNotNull(psa[0]); Section s = (Section)(psa[0].Sections[0]); Assert.AreEqual(s.FormatID, formatID); Object p = s.GetProperty(2); Assert.AreEqual(SECTION1, p); s = (Section)(psa[0].Sections[1]); p = s.GetProperty(2); Assert.AreEqual(SECTION2, p); file.Close(); //File.Delete(dataDir + POI_FS); }
public void TestWithoutAFormatID() { FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc"); using (FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite)) { //FileStream filename = File.OpenRead(dataDir + POI_FS); //filename.deleteOnExit(); /* Create a mutable property Set with a section that does not have the * formatID Set: */ FileStream out1 = file; POIFSFileSystem poiFs = new POIFSFileSystem(); MutablePropertySet ps = new MutablePropertySet(); ps.ClearSections(); ps.AddSection(new MutableSection()); /* Write it to a POIFS and the latter to disk: */ try { MemoryStream psStream = new MemoryStream(); ps.Write(psStream); psStream.Close(); byte[] streamData = psStream.ToArray(); poiFs.CreateDocument(new MemoryStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.WriteFileSystem(out1); out1.Close(); Assert.Fail("Should have thrown a NoFormatIDException."); } catch (Exception ex) { Assert.IsTrue(ex is NoFormatIDException); } finally { poiFs.Close(); out1.Close(); } } try { File.Delete(fi.FullName); } catch { } }