C# 合并两个文件并处理重复条目
编辑2:如果您希望获得更多代码,请告诉我。但是,我真的不知道您还需要什么代码,因为问题显然在于下面的代码(不是我的编辑) 编辑:如果还有人对这个问题的答案感兴趣,我还没有完全弄明白,但注意到了一些相当奇怪的事情。在我看来,C# 合并两个文件并处理重复条目,c#,file,io,merge,C#,File,Io,Merge,编辑2:如果您希望获得更多代码,请告诉我。但是,我真的不知道您还需要什么代码,因为问题显然在于下面的代码(不是我的编辑) 编辑:如果还有人对这个问题的答案感兴趣,我还没有完全弄明白,但注意到了一些相当奇怪的事情。在我看来,List[index].Replace(oldValue,newValue)(通用格式)对我来说根本不起作用!这就是我问题的最后一部分所在。我已经添加了断点并一步一步地调试了我的应用程序,当卡已经存在(newsflash,它确实存在)但命令仍然不想工作时,我检查了newFile
List[index].Replace(oldValue,newValue)代码>(通用格式)对我来说根本不起作用!这就是我问题的最后一部分所在。我已经添加了断点并一步一步地调试了我的应用程序,当卡已经存在(newsflash,它确实存在)但命令仍然不想工作时,我检查了newFile是否确实包含行[I]
<代码>替换()代码>实际上是无用的。为什么呢?我的代码有问题吗?见鬼,我甚至分解了这个片段:
newFile[newFile.IndexOf(lines[i])].Replace(lines[i],
string.Format(
"{0}|{1}", title, int.Parse(times) + int.Parse(secondTimes)));
更简单的是(仅为了评估目的,为了查看元素是否被替换,当i==0时,请注意,Winged Dragon是newFile的第一个(数字0)元素):
但它仍然拒绝工作!顺便说一句,我为愤怒感到抱歉,但我不能,为了我的生命,理解我在这里做错了什么
昨天我问了一个类似的问题(可以在这里找到:),我得到了我想要的答案。但是,现在我想扩展这个功能,允许我的应用程序将两个文件合并为一个,同时考虑重复条目。同样,所有给定文件的每一行的一般格式是string.format(“{0}{1}”,TitleOfCard,numberOfTimesCardSowned)代码>
在开始编写代码之前,您可能想知道,我让用户通过OpenFileDialog(Multiselect=True)浏览要合并的文件
现在,这是我的代码:
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);
}
}
}
private void btnMerge\u单击(对象发送者,事件参数e)
{
如果(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//将文件名保存到数组。
string[]fileNames=openFileDialog1.fileNames;
//循环浏览文件。
foreach(文件名中的字符串文件名)
{
//将当前文件的所有行保存到数组中。
string[]lines=File.ReadAllLines(文件名);
//循环浏览文件的行。
对于(int i=0;i保留标题,更新卡片拥有的次数
newFile[newFile.IndexOf(第[i]行])。替换(第[i]行),
字符串格式(
“{0}{1}”,标题,int.Parse(times)+int.Parse(secondTimes));
}
//如果newFile不包含我们正在循环的文件的当前行,只需将其添加到newFile。
其他的
newFile.Add(string.Format(
"{0}|{1}",
名称,时间);;
}
}
//用newFile覆盖资源文件。
使用(StreamWriter sw=new StreamWriter(“CardResources.ygodc”))
{
foreach(新文件中的字符串行)
西南写入线(行);
}
//清除listview并重新更新它vv
lvResources.Clear();
string[]originalFile=File.ReadAllLines(“CardResources.ygodc”);
foreach(原始文件中的字符串行)
{
string[]split=line.split(“|”);
字符串标题=拆分[0];
字符串时间=拆分[1];
ListViewItem lvi=新的ListViewItem(标题);
lvi.SubItems.Add(次);
lvResources.Items.Add(lvi);
}
}
}
我遇到问题的行是string times=split[1]当我尝试合并文件时,会抛出代码>作为一个IndexOutOfRangeException。你能帮帮我吗?提前感谢。您很可能正在尝试处理空行
要忽略空行,请执行以下操作:
if (lines[i].Trim() == string.Empty)
continue;
string[] split = lines[i].Split('|');
if (split.Length != 2)
throw new InvalidOperationException("invalid file");
string title = split[0];
string times = split[1];
要忽略无效行,请执行以下操作:
string[] split = lines[i].Split('|');
if (split.Length != 2)
continue;
string title = split[0];
string times = split[1];
发生异常时,行[i]
的内容是什么?请注意,所有这些图片和GUI细节都不会对字符串产生影响。拆分问题。只需忽略意外情况?取决于OP想要什么,是的。如果OP认为这应该是一个意外情况,那么应该抛出一个异常(您的答案)。我更喜欢这种方法,因为首先抛出异常的原因可能是一个bug(原始文件的保存方式或我从中读取的方式)。话虽如此,我还是
string[] split = lines[i].Split('|');
if (split.Length != 2)
continue;
string title = split[0];
string times = split[1];