public static List <LogInfo> IniAddSectionOp(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_IniAddSectionOp infoOp = cmd.Info.Cast <CodeInfo_IniAddSectionOp>(); string fileName = StringEscaper.Preprocess(s, infoOp.Infos[0].FileName); Debug.Assert(fileName != null, $"{nameof(fileName)} != null"); if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg)) { return(LogInfo.LogErrorMessage(logs, errorMsg)); } string[] sections = new string[infoOp.Cmds.Count]; for (int i = 0; i < sections.Length; i++) { CodeInfo_IniAddSection info = infoOp.Infos[i]; string sectionName = StringEscaper.Preprocess(s, info.Section); if (sectionName.Length == 0) { return(LogInfo.LogErrorMessage(logs, "Section name cannot be empty")); } sections[i] = sectionName; } string dirPath = Path.GetDirectoryName(fileName); if (dirPath == null) { throw new InternalException("Internal Logic Error at IniAddSectionOp"); } if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } // If a dest file does not exist, create an empty file to force ANSI encoding as default in IniReadWriter. if (!File.Exists(fileName)) { File.Create(fileName).Dispose(); } bool result = IniReadWriter.AddSections(fileName, sections); if (result) { for (int i = 0; i < sections.Length; i++) { logs.Add(new LogInfo(LogState.Success, $"Section [{sections[i]}] added", infoOp.Cmds[i])); } logs.Add(new LogInfo(LogState.Success, $"Added [{sections.Length}] sections to [{fileName}]", cmd)); } else { for (int i = 0; i < sections.Length; i++) { logs.Add(new LogInfo(LogState.Error, $"Could not add section [{sections[i]}]", infoOp.Cmds[i])); } logs.Add(new LogInfo(LogState.Error, $"Could not add [{sections.Length}] sections to [{fileName}]", cmd)); } return(logs); }