比较c#中的2个数组

时间:2014-08-11 10:45:47

标签: c# arrays

大家好我需要比较两个文本文件,例如:

File1:

MB-ASUSZ97K 
GPU-GTX780 
snd-sbaudigyfx 
WLAN-300 

file2:

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并获取从服务器复制驱动程序到hdd的链接。 我想我需要3个数组来移动它的链接。 我的静止是如何引用它们来获取链接?

这是我到目前为止(在answer below的帮助下):

void getDrivers()
{
    string path1 = "c:/testbase/65500.txt";
    string path2 = "c:/testbase/config.txt";

    var file1 = new HashSet<string>(File.ReadAllLines(path1));
    string line;
    var file2 = new System.IO.StreamReader(path2);
    Dictionary<string, string> links = new Dictionary<string, string>();
    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: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780      Value://SERVER/drivers/gtx780.exe
Key: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780      Value://SERVER/drivers/gtx780.exe
Key: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780      Value://SERVER/drivers/gtx780.exe
Key: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: snd-sbaudigyfx  Value://SERVER/drivers/sbfx.exe
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: MB-ASUSZ97K     Value://SERVER/drivers/asusz97k.exe
Key: GPU-GTX780      Value://SERVER/drivers/gtx780.exe
Key: snd-sbaudigyfx  Value://SERVER/drivers/sbfx.exe
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

但需要这个:

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

1 个答案:

答案 0 :(得分:0)

所以第一个文件只包含名称,而第二个文件包含名称后跟链接?

这应该有效:

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