private static void ParseDiffResult(string content, string ignoreRegex, ref FileDiffResult result)
        {
            result.Differences = new List <Differences>();

            var expFile = Path.Combine(result.ExpectDirectory, result.FileName);
            var tarFile = Path.Combine(result.TargetDirectory, result.FileName);

            var diffSection = GetDifferenceSection(content);

            var ignoreRegexArray = ignoreRegex.Split(new[] { '|' });

            foreach (var sec in diffSection)
            {
                if (!string.IsNullOrEmpty(sec))
                {
                    var diff = new Differences();

                    var expectfilediffstartpos = sec.ToLower().IndexOf(result.FileName.ToLower());
                    if (expectfilediffstartpos < 0)
                    {
                        continue;
                    }
                    expectfilediffstartpos += result.FileName.Length;
                    var expectfilediffendpos = sec.IndexOf("*****", expectfilediffstartpos);
                    if (expectfilediffendpos < 0)
                    {
                        continue;
                    }

                    diff.Expect = sec.Substring(expectfilediffstartpos, expectfilediffendpos - expectfilediffstartpos).Trim();

                    // Check the expected string in difference is in Ignore List
                    var isIgnore = false;
                    if (!string.IsNullOrEmpty(diff.Expect))
                    {
                        isIgnore = ignoreRegexArray.Any(reg => !string.IsNullOrEmpty(reg) && Regex.IsMatch(diff.Expect, reg, RegexOptions.IgnoreCase));
                    }

                    if (isIgnore)
                    {
                        continue;
                    }

                    var targetfilediffstartpos = sec.ToLower().IndexOf(result.FileName.ToLower(), expectfilediffendpos);
                    if (targetfilediffstartpos < 0)
                    {
                        continue;
                    }
                    targetfilediffstartpos += result.FileName.Length;
                    var targetexpectfilediffendpos = sec.IndexOf("*****", targetfilediffstartpos);
                    if (targetexpectfilediffendpos < 0)
                    {
                        continue;
                    }

                    diff.Target = sec.Substring(targetfilediffstartpos, targetexpectfilediffendpos - targetfilediffstartpos).Trim();

                    diff.Reason = AnalysisDiffResultType(ref diff.Expect, ref diff.Target);

                    result.Differences.Add(diff);
                }
            }
        }
        /// <summary>
        /// Compare 2 folders
        /// </summary>
        /// <param name="expectedFolder">the expected folder as the baseline</param>
        /// <param name="targetFolder">target folder</param>
        /// <param name="binaryFileExtensions">list all binary file extension, format: .exe,.dll,.rm</param>
        /// <param name="ignoreFileExtensions">list all file extensions which the file will be ignored, format: .log,.inf,.trc </param>
        /// <param name="isScanRecursiveDirectory">also scan all sub directory</param>
        /// <param name="isCaseSensitive">case sensitive</param>
        /// <param name="isCompressWhiteSpace">compress tab, space</param>
        /// <param name="ignoreRegex"></param>
        /// <param name="reportFile">save the result to XML file.</param>
        /// <param name="alwaysGenerateReport">if need to generate report even they are equals</param>
        /// <param name="ignoreLineSwitchFiles">files which with line switch difference will be ginored, format: action.c,default.usr,default.c</param>
        /// <param name="skipFiles">list all files which need to be skipped during comparing</param>
        /// <returns></returns>
        public static bool CompareFolder(string expectedFolder, string targetFolder, string binaryFileExtensions, string ignoreFileExtensions,
                                         bool isScanRecursiveDirectory, bool isCaseSensitive, bool isCompressWhiteSpace, string ignoreRegex, string reportFile, bool alwaysGenerateReport, string[] ignoreLineSwitchFiles, params string[] skipFiles)
        {
            var result = true;

            var resultfile = string.IsNullOrEmpty(reportFile) ? string.Format("Result_{0:yyyyMMddhhmmss}.xml", DateTime.Now) : reportFile;

            var fileDifferences = new List <FileDiffResult>();

            var bFileExt = binaryFileExtensions == null ? null : binaryFileExtensions.Split(new[] { ',' });
            var iFileExt = ignoreFileExtensions == null ? null : ignoreFileExtensions.Split(new[] { ',' });

            foreach (var expectedFile in Directory.GetFiles(expectedFolder, "*.*", isScanRecursiveDirectory ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
            {
                var cmpType  = 1;
                var ext      = Path.GetExtension(expectedFile);
                var filename = Path.GetFileName(expectedFile);
                var subpath  = expectedFile.Substring(expectedFolder.Length + 1, expectedFile.IndexOf(filename) - expectedFolder.Length - 1);

                // If it is an ignore type of files, skip it.
                if (iFileExt != null)
                {
                    if (iFileExt.Contains(ext))
                    {
                        continue;
                    }
                }

                // If it is a skip file, skip it.
                if (skipFiles != null)
                {
                    if (skipFiles.Where(o => expectedFile.ToLower().IndexOf(o.ToLower()) > 0).Count() > 0)
                    {
                        continue;
                    }
                }

                // If it is a binary file, compare it by Binary
                if (bFileExt != null)
                {
                    if (bFileExt.Contains(ext))
                    {
                        cmpType = 0;
                    }
                }

                // Combine and Get the target file.
                var targetFile = string.Format(@"{0}\{1}\{2}", targetFolder, subpath, filename).Replace(@"\\", @"\");

                var fdr = new FileDiffResult
                {
                    FileName        = filename,
                    ExpectDirectory = expectedFile.Remove(expectedFile.IndexOf(filename)),
                    TargetDirectory = targetFile.Remove(targetFile.IndexOf(filename))
                };

                // If file2 does not exist, return error. Otherwise, compare them.
                if (!File.Exists(targetFile))
                {
                    fdr.Differences = new List <Differences>();
                    var diff = new Differences {
                        Reason = DiffReason.FileMissing
                    };
                    fdr.Differences.Add(diff);
                    fileDifferences.Add(fdr);
                }
                else
                {
                    string diffoutput;
                    result &= Fc(expectedFile, targetFile, cmpType, isCaseSensitive, isCompressWhiteSpace, out diffoutput);
                    if (!string.IsNullOrEmpty(diffoutput))
                    {
                        if (cmpType == 0)
                        {
                            fdr.Differences = new List <Differences>();
                            var diff = new Differences {
                                Reason = DiffReason.BinaryFileLengthDifferent
                            };
                            fdr.Differences.Add(diff);
                            fileDifferences.Add(fdr);
                        }
                        else if (cmpType == 1)
                        {
                            ParseDiffResult(diffoutput, ignoreRegex, ref fdr);
                            if (fdr.Differences.Count > 0)
                            {
                                if (ignoreLineSwitchFiles != null)
                                {
                                    if (ignoreLineSwitchFiles.Contains(filename))
                                    {
                                        continue;
                                    }
                                }
                                fileDifferences.Add(fdr);
                            }
                        }
                    }
                }
            }

            if (fileDifferences.Count > 0)
            {
                result = false;
                Console.WriteLine("There are {0} files different.", fileDifferences.Count);
                foreach (var fd in fileDifferences)
                {
                    Console.WriteLine(fd.FileName);
                }
            }
            else
            {
                result = true;
            }


            if (!result || (result && alwaysGenerateReport))
            {
                Console.WriteLine("Result File: {0}", resultfile);
                GenerateReport(resultfile,
                               result,
                               expectedFolder,
                               targetFolder,
                               binaryFileExtensions,
                               ignoreFileExtensions,
                               isScanRecursiveDirectory,
                               isCaseSensitive,
                               isCompressWhiteSpace,
                               skipFiles,
                               fileDifferences);
            }
            else
            {
                Console.WriteLine("Result: No difference in content.");
            }

            return(result);
        }