EDIT 2: If you'd like to be provided with more code, then just let me know. But, I don't really know what other code you would want, as the problem obviously lies in the code below (not my edit).
EDIT: If anyone's still interested in the answer to this question, I haven't exactly figured it out but noticed something rather bizarre. It seems to me List[index].Replace(oldValue, newValue);
(general format) will not work AT ALL for me! This is where the last part of my problem lies. I've added breakpoints and debugged my application step by step, checked if newFile indeed contains lines[i]
when the card already exists (newsflash, it does) but still the command doesn't want to work! Replace();
is practically useless. Why is that? Is there something wrong with my code? Hell, I've even broken down this snippet:
newFile[newFile.IndexOf(lines[i])].Replace(lines[i],
string.Format(
"{0}|{1}", title, int.Parse(times) + int.Parse(secondTimes)));
to the much simpler (just for evaluating purposes, to see if the element actually gets replaced, when i == 0, Winged Dragon is the first (number 0) element of newFile, mind you):
newFile[0].Replace("Winged Dragon, Guardian of the Fortress #1|3", "Just replace it with this dammit!");
But it still refuses to work! Sorry for raging, by the way, but I can't, for the life of me, understand what I'm doing wrong here.
Yesterday I asked a similar question (which can be found here: Writing to a file and handling duplicate entries) and I got the answer I wanted. However, now I'd like to expand on that functionality by allowing my application to merge two files into one, taking into account duplicate entries. Once again, the general format of each line of all given files is string.Format("{0}|{1}", TitleOfCard, NumberOfTimesCardIsOwned);
.
Before I get to the code, you may want to know that I let the user browse for the file(s) to merge through an OpenFileDialog (Multiselect=True).
Now, here is my code:
private void btnMerge_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Save file names to array.
string[] fileNames = openFileDialog1.FileNames;
// Loop through the files.
foreach (string fileName in fileNames)
{
// Save all lines of the current file to an array.
string[] lines = File.ReadAllLines(fileName);
// Loop through the lines of the file.
for (int i = 0; i < lines.Length; i++)
{
// Split current line ( remember, all lines are of format {0}|{1} ).
string[] split = lines[i].Split('|');
string title = split[0]; //
// = TitleOfCard, NumberOfTimesCardIsOwned
string times = split[1]; //
// newFile is a list that stores the lines from the default resources file.
// If it contains the current line of the file that we're currently looping through...
if (newFile.Contains(lines[i]))
{
// Split the line once again.
string[] secondSplit = newFile.ElementAt(
newFile.IndexOf(lines[i])).Split('|');
string secondTitle = secondSplit[0];
string secondTimes = secondSplit[1];
// Replace the newFile element at the specified index with:
// - Same title
// - Add the 'times' of the newFile element and the 'times' of the newFile
// => KEEP TITLE, UPDATE THE NUMBER OF TIMES CARD IS OWNED
newFile[newFile.IndexOf(lines[i])].Replace(lines[i],
string.Format(
"{0}|{1}", title, int.Parse(times) + int.Parse(secondTimes)));
}
// If newFile does not contain the current line of the file we're looping through, just add it to newFile.
else
newFile.Add(string.Format(
"{0}|{1}",
title, times));
}
}
// Overwrite resources file with newFile.
using (StreamWriter sw = new StreamWriter("CardResources.ygodc"))
{
foreach (string line in newFile)
sw.WriteLine(line);
}
// Clear listview and reupdate it vv
lvResources.Clear();
string[] originalFile = File.ReadAllLines("CardResources.ygodc");
foreach (string line in originalFile)
{
string[] split = line.Split('|');
string title = split[0];
string times = split[1];
ListViewItem lvi = new ListViewItem(title);
lvi.SubItems.Add(times);
lvResources.Items.Add(lvi);
}
}
}
The line I'm having trouble with is string times = split[1];
as an IndexOutOfRangeException is thrown when I try to merge the files. Can you please help me? Thanks in advance.