Beispiel #1
0
        /// <summary>
        /// Reads the text content of a file and determines its Encoding.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="ignoreInsignificantWhiteSpace"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public static FileContentInfo GetXmlText(string fileName,
                                                 bool ignoreInsignificantWhiteSpace,
                                                 IDiffProgress progress)
        {
            var contentInfo = new FileContentInfo();

//// This should be created from in-memory text to save IO and support editing
////			using (StreamReader reader = new StreamReader(fileName, Encoding.Default, true))
////			{
////				contentInfo.Lines = GetXmlTextLines(reader, ignoreInsignificantWhiteSpace, progress);
////			}

            // Read the RAW text content
            const int         DefaultBufferSize = 4096;
            const FileOptions DefaultOptions    = FileOptions.Asynchronous | FileOptions.SequentialScan;

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
            {
                var bom = new byte[4];                               // Decode bom (if any) and continue to read text content
                stream.Read(bom, 0, 4);
                stream.Seek(0, SeekOrigin.Begin);

                contentInfo.TextEncoding = FileEx.GetEncoding(bom);

                using (StreamReader reader = new StreamReader(stream, contentInfo.TextEncoding))
                {
                    contentInfo.TextContent = reader.ReadToEnd();
                }
            }

            return(contentInfo);
        }