Краткое описание вопроса. В моей программе я создаю 5 ключей в словаре, и каждый ключ словаря получает значение из списка. В конце я записываю содержимое 5 ключей в файл. Если я использую список, запишите значения в списке, поместите список в key1, очистите список, чтобы использовать его повторно для key2 и т. Д., В конце в файле все 5 ключей имеют те же значения, что и последний ключ. , Однако, если я использую новый список для каждого из 5 ключей, то я получаю в файл то, что ожидаю получить.
А теперь код.
List<string> tempList = new List<string>();
tempList.Clear(); foreach (string item in PSD.labadvancedPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("labadvancedPathS", tempList, false, true);
tempList.Clear(); foreach (string item in PSD.xmlCleanupBoolString) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("xmlCleanupBoolString", tempList, false, true);
tempList.Clear(); foreach (string item in PSD.rmiPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("rmiPathS", tempList, false, true);
tempList.Clear(); foreach (string item in PSD.txtCleanupBoolString) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("txtCleanupBoolString", tempList, false, true);
tempList.Clear(); foreach (string item in PSD.xmlPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("xmlPathS", tempList, false, true);
tempList.Clear(); foreach (string item in PSD.backupJobs) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("backupJobs", tempList, false, true);
.
.
.
_iniFile.UpdateIniFile(true);
Примечание: В каждый Debug.WriteLine () Я получаю ожидаемый вывод из tempList
Теперь для части _iniFileSetValueMultiLine ().
public class IniFile
{
private Dictionary<string, List<String>> settingsMultiLine = new Dictionary<string, List<String>>();
public void SetValueMultiLine(string settingName, List<String> settingValue, bool updateTheIniFile = false, bool debugMessages = false)
{
if (settingsMultiLine.ContainsKey(settingName))
{
this.settingsMultiLine[settingName] = settingValue;
if (debugMessages)
{
MessageBox.Show("Found the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
}
}
else
{
settingsMultiLine.Add(settingName, settingValue);
if (debugMessages)
{
MessageBox.Show("Creating the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
}
if (!messageWasShown)
{
showErrorMessage(settingName + " was not found. Creating the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
}
}
if (updateTheIniFile) { this.UpdateIniFile(); }
}
}
Примечание2: опять в Messagebox.show (), Я получаю ожидаемый вывод из имени и значения параметра
И, наконец, часть, в которую я записываю файл:
public void UpdateIniFile(bool debugMessages = false)
{
List<string> whatToWriteToFile = new List<string>();
foreach (string item in settingsMultiLine.Keys)//Enumerate through the multilined Settings
{
whatToWriteToFile.Add("[" + item + " START]");
if (debugMessages) { MessageBox.Show("[" + item + " START]"); }
foreach (string subItem in settingsMultiLine[item])//Enumerate through the values of the multilined setting
{
whatToWriteToFile.Add(subItem);
if (debugMessages) { MessageBox.Show(subItem); }
}
whatToWriteToFile.Add("[" + item + " END]");
if (debugMessages) { MessageBox.Show("[" + item + " END]"); }
whatToWriteToFile.Add("");
}
foreach (string item in settingsSingleLine.Keys)
{
whatToWriteToFile.Add("[" + item + "]");
whatToWriteToFile.Add(this.settingsSingleLine[item]);
whatToWriteToFile.Add("");
}
File.WriteAllLines(iniFileFullPath, whatToWriteToFile);
}
И вот здесь происходит «что-то», и 5 ключи в файле те же, что и у последнего, а также в сообщениях отображается «неправильная» информация.
Спасибо, что читаете меня, пожалуйста, будьте осторожны со мной:)