ここ数日、つまり、すべての質問で少し苦労していたことは知っていますが、私はこのプロジェクトを開発しており、(比喩的に)完成まであと数インチです。
そうは言っても、もう 1 つの問題であなたの助けが必要です。以前の質問に関連していますが、それらのコードは必要ありません。問題はまさにこのコードにあります。私があなたに望んでいるのは、それを特定し、結果的に解決するのを手伝ってくれることです.
私が取り組んできたコードをお見せする前に、いくつか付け加えておきたいことがあります。
- 私のアプリケーションにはファイル マージ機能があり、2 つのファイルをマージして重複したエントリを処理します。
- どのファイルでも、各行は次の 4 つの形式のいずれかを持つことができます (最後の 3 つはオプションです):
Card Name|Amount
、.Card Name|Amount
、..Card Name|Amount
、_Card Name|Amount
. - 行が適切にフォーマットされていない場合、プログラムはそれをスキップします (完全に無視します)。
したがって、基本的に、サンプル ファイルは次のようになります。
Blue-Eyes White Dragon|3
..Blue-Eyes Ultimate Dragon|1
.Dragon Master Knight|1
_Kaibaman|1
さて、ファイル マージャーの使用に関しては、行が特殊文字の 1 つで始まる場合、. .. _
それに応じて動作する必要があります。の場合.
、正常に動作します。で始まる行について..
は、インデックスを 2 番目のドットに移動し、最後に_
行を完全に無視します (この説明とは関係のない別の用途があります)。
マージ関数のコードは次のとおりです (奇妙な理由で、2 番目のループ内のコードはまったく実行されません)。
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.
string[] split = lines[i].Split('|');
// If the current line is badly formatted, skip to the next one.
if (split.Length != 2)
continue;
string title = split[0];
string times = split[1];
if (lines[i].StartsWith("_"))
continue;
// If newFile (list used to store contents of the card resource file) contains the current line of the file that we're currently looping through...
for (int k = 0; k < newFile.Count; k++)
{
if (lines[i].StartsWith(".."))
{
string newTitle = lines[i].Substring(
lines[i].IndexOf("..") + 1);
if (newFile[k].Contains(newTitle))
{
// Split the line once again.
string[] secondSplit = newFile.ElementAt(
newFile.IndexOf(newFile[k])).Split('|');
string secondTimes = secondSplit[1];
// Replace the newFile element at the specified index.
newFile[newFile.IndexOf(newFile[k])] =
string.Format("{0}|{1}", newTitle, 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}",
newTitle, times));
continue;
}
if (newFile[k].Contains(title))
{
string[] secondSplit = newFile.ElementAt(
newFile.IndexOf(newFile[k])).Split('|');
string secondTimes = secondSplit[1];
newFile[newFile.IndexOf(newFile[k])] =
string.Format("{0}|{1}", title, int.Parse(times) + int.Parse(secondTimes));
}
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);
}
これは非常に長いコードであることは承知していますが、そのすべてはある点に関連していると思います。完全に無関係なので、いくつかの重要でないビットをスキップしました (これがすべて実行された後)。