private static void FilterIniFile(string SourceName, string TargetName, List <string> IniKeyBlacklist, List <string> InSectionBlacklist) { string[] Lines = File.ReadAllLines(SourceName); StringBuilder NewLines = new StringBuilder(""); bool bFilteringSection = false; foreach (string OriginalLine in Lines) { string Line = OriginalLine.Trim(); bool bFiltered = bFilteringSection; // look for each filter on each line if (!bFiltered) { string TrimmedLine = Line.TrimStart(IgnoredIniValuePrefixes); foreach (string Filter in IniKeyBlacklist) { if (TrimmedLine.StartsWith(Filter + "=")) { bFiltered = true; break; } } } if (InSectionBlacklist != null) { if (Line.StartsWith("[") && Line.EndsWith("]")) { string SectionName = Line.Substring(1, Line.Length - 2); bFilteringSection = bFiltered = InSectionBlacklist.Contains(SectionName); if (bFilteringSection) { Log.TraceLog("Filtering config section '{0}'", SectionName); } } } // write out if it's not filtered out if (!bFiltered) { NewLines.AppendLine(Line); } } // now write out the final .ini file if (File.Exists(TargetName)) { File.Delete(TargetName); } File.WriteAllText(TargetName, NewLines.ToString()); // other code assumes same timestamp for source and dest File.SetLastWriteTimeUtc(TargetName, File.GetLastWriteTimeUtc(SourceName)); }