C#异常处理继续出错

Max*_*eek 2 c# csv exception

我有一个基本的C#控制台应用程序,它逐行读取文本文件(CSV格式)并将数据放入HashTable.该行中的第一个CSV项是键(id num),该行的其余部分是值.但是我发现我的导入文件有一些不应该有的重复键.当我尝试导入文件时,应用程序错误,因为您不能在HashTable中有重复的键.我希望我的程序能够处理此错误.当我遇到一个重复的密钥时,我想将该密钥放入一个arraylist并继续将其余的数据导入到哈希表中.我怎么能在C#中做到这一点

这是我的代码:


private static Hashtable importFile(Hashtable myHashtable,String myFileName){

        StreamReader sr = new StreamReader(myFileName);
        CSVReader csvReader = new CSVReader();
        ArrayList tempArray = new ArrayList();
        int count = 0;

        while (!sr.EndOfStream)
        {
            String temp = sr.ReadLine();
            if (temp.StartsWith(" "))
            {
                ServMissing.Add(temp);
            }
            else
            {
                tempArray = csvReader.CSVParser(temp);
                Boolean first = true;
                String key = "";
                String value = "";

                foreach (String x in tempArray)
                {
                    if (first)
                    {
                        key = x;
                        first = false;
                    }
                    else
                    {
                        value += x + ",";
                    }
                }
                myHashtable.Add(key, value);
            }
            count++;
        }

        Console.WriteLine("Import Count: " + count);
        return myHashtable;
    }
Run Code Online (Sandbox Code Playgroud)

jop*_*jop 10

if (myHashtable.ContainsKey(key))
    duplicates.Add(key);
else
    myHashtable.Add(key, value);
Run Code Online (Sandbox Code Playgroud)