Beispiel #1
0
        public override string ReadContents()
        {
            string documentContent = null;

            Stream stream = new MemoryStream(File.ReadAllBytes(_FilePath));

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, false))
            {
                using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                {
                    documentContent = sr.ReadToEnd();

                    // Now search any Comments in the document as well.
                    WordprocessingCommentsPart wordProcessingCommentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart;
                    if (wordProcessingCommentsPart != null)
                    {
                        using (StreamReader sr1 = new StreamReader(wordProcessingCommentsPart.GetStream()))
                        {
                            documentContent += sr1.ReadToEnd();
                        }
                    }
                }
            }
            return(documentContent);
        }
Beispiel #2
0
        private byte[] ReadExactly(string path, int offset, int size)
        {
            //if the file length is less than offset+size then always just return
            //the whole file.  sometimes this will mean prehash is a bit
            //longer to run than with just 1KB designed for but still a trivial
            //time frame.  Done this way for simplicity.

            FileInfo file = new FileInfo(path);

            if (file.Length <= (offset + size))
            {
                return(File.ReadAllBytes(path));
            }

            using (System.IO.Stream stream = File.OpenRead(path))
            {
                stream.Position = offset;
                byte[] buffer    = new byte[size];
                int    bytesRead = 0;
                do
                {
                    bytesRead += stream.Read(buffer, bytesRead, size);
                    size      -= bytesRead;
                } while (size > 0);
                return(buffer);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Reads the contents of a 2007+ Excel file and returns the contents as a single string.
        /// </summary>
        /// <returns>Contents of any 2007+ Excel files (.xlsx etc) as a single string</returns>
        public override string ReadContents()
        {
            Stream stream = new MemoryStream(File.ReadAllBytes(_FilePath));

            DataTableCollection worksheets;
            StringBuilder       sb = new StringBuilder();

            using (IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream))
            {
                worksheets = excelReader.AsDataSet().Tables;
                excelReader.Close();
            }

            foreach (DataTable sheet in worksheets)
            {
                foreach (DataRow row in sheet.Rows)
                {
                    foreach (var cell in row.ItemArray)
                    {
                        sb.Append(cell.ToString());
                    }
                }
            }

            return(sb.ToString());
        }
Beispiel #4
0
        public void TestWriteAllBytes()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();
            var expected             = new byte[] { 3, 4, 1, 5, 9, 2, 6, 5 };

            File.WriteAllBytes(tempLongPathFilename, expected);
            try
            {
                Assert.IsTrue(expected.SequenceEqual(File.ReadAllBytes(tempLongPathFilename)));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Beispiel #5
0
        public void TestReadAllBytes()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename))
            {
                fileStream.WriteByte(42);
            }
            try
            {
                Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename)));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Beispiel #6
0
        public void TestReadAllBytesOnHugeFile()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename))
            {
                fileStream.Seek(((long)int.MaxValue) + 1, SeekOrigin.Begin);
                fileStream.WriteByte(42);
            }
            Assert.Throws <IOException>(() =>
            {
                try
                {
                    Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename)));
                }
                finally
                {
                    File.Delete(tempLongPathFilename);
                }
            });
        }
Beispiel #7
0
 public static byte[] ReadAllBytes(string path)
 {
     return(File.ReadAllBytes(path));
 }