C# 比较c语言中的两个数组#
大家好,我需要比较两个文本文件,即: 文件1:C# 比较c语言中的两个数组#,c#,arrays,C#,Arrays,大家好,我需要比较两个文本文件,即: 文件1: MB-ASUSZ97K GPU-GTX780 snd-sbaudigyfx WLAN-300 文件2: MB-ASUSZ97K //SERVER/drivers/asusz97k.exe PCI-FWI121 //SERVER/drivers/fwi.exe GPU-GTX780 //SERVER/drivers/gtx780.exe SOF-BULL1 //SERVER/drivers/bullguard.exe snd-s
MB-ASUSZ97K
GPU-GTX780
snd-sbaudigyfx
WLAN-300
文件2:
MB-ASUSZ97K
//SERVER/drivers/asusz97k.exe
PCI-FWI121
//SERVER/drivers/fwi.exe
GPU-GTX780
//SERVER/drivers/gtx780.exe
SOF-BULL1
//SERVER/drivers/bullguard.exe
snd-sbaudigyfx
//SERVER/drivers/sbfx.exe
Z97N-WIFI
//SERVER/drivers/z97nwifi.exe
WLAN-300
//SERVER/drivers/wlan300.exe
我所需要做的就是将file1引用到file2,并获取将驱动程序从服务器复制到硬盘的链接。
我想我需要3个数组来移动到它的链接。
我的问题是如何引用它们来获取链接
以下是我到目前为止(在
但我们需要:
Key: MB-ASUSZ97K Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780 Value://SERVER/drivers/gtx780.exe
Key: snd-sbaudigyfx Value://SERVER/drivers/sbfx.exe
Key: WLAN-300 Value://SERVER/drivers/wlan300.exe
那么第一个文件只包含名称,而第二个文件包含名称,后面是链接 这应该起作用:
var links = new Dictionary<string, string>();
var file1 = new HashSet<string>(File.ReadLines(path1));
var file2 = new System.IO.StreamReader(path2);
string line;
while ((line = file2.ReadLine()) != null)
{
string nextLine;
if (file1.Contains(line) && (nextLine = file2.ReadLine()) != null)
links[line] = nextLine;
}
产出:
Key: MB-ASUSZ97K Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780 Value://SERVER/drivers/gtx780.exe
Key: snd-sbaudigyfx Value://SERVER/drivers/sbfx.exe
Key: WLAN-300 Value://SERVER/drivers/wlan300.exe
从文件2创建字典并从文件1中查找项目?下面的答案有效,但没有给出所需的输出。仍在进行中,尚未完全分类。它列出了所有内容,但有多行。MB。。。重复4次,然后出现混合MB。。。和GPU。。。和SND。。。最后我得到了我想要的最后4行。英语也不是我的母语。对不起,如果你不理解我,尽我最大的努力@arti:编辑您的问题并提供演示此问题的示例数据。还显示了所需的结果。如果你已经编辑了它,你可以在这里留下评论,这样我就可以看一看。我想要的输出是你提供的,但我得到的是:哦,我太傻了,对不起!正如您在我的代码中看到的,foreach循环在while循环中。我发现了我的问题。谢谢你的帮助!
var links = new Dictionary<string, string>();
var file1 = new HashSet<string>(File.ReadLines(path1));
var file2 = new System.IO.StreamReader(path2);
string line;
while ((line = file2.ReadLine()) != null)
{
string nextLine;
if (file1.Contains(line) && (nextLine = file2.ReadLine()) != null)
links[line] = nextLine;
}
foreach (var kv in links)
Console.WriteLine("Key: {0} Value:{1}", kv.Key, kv.Value);
Key: MB-ASUSZ97K Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780 Value://SERVER/drivers/gtx780.exe
Key: snd-sbaudigyfx Value://SERVER/drivers/sbfx.exe
Key: WLAN-300 Value://SERVER/drivers/wlan300.exe