Compare two csv file using in C#











up vote
1
down vote

favorite
1












I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?



Please help me.










share|improve this question






















  • What format do you need the differences to be in?
    – Oded
    Mar 1 '11 at 12:16










  • If you need differnce in data, than yes you need. If you just need to know if files are different than no
    – Stecya
    Mar 1 '11 at 12:16






  • 1




    Read both csv file into list and then apply Linq set operation
    – santosh singh
    Mar 1 '11 at 12:16










  • without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
    – Terry
    Mar 1 '11 at 12:17










  • You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
    – Security Hound
    Mar 1 '11 at 12:17

















up vote
1
down vote

favorite
1












I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?



Please help me.










share|improve this question






















  • What format do you need the differences to be in?
    – Oded
    Mar 1 '11 at 12:16










  • If you need differnce in data, than yes you need. If you just need to know if files are different than no
    – Stecya
    Mar 1 '11 at 12:16






  • 1




    Read both csv file into list and then apply Linq set operation
    – santosh singh
    Mar 1 '11 at 12:16










  • without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
    – Terry
    Mar 1 '11 at 12:17










  • You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
    – Security Hound
    Mar 1 '11 at 12:17















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?



Please help me.










share|improve this question













I need to find out difference between two csv files programatically.
Is there any way to find out the difference without using any loops?



Please help me.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 1 '11 at 12:15









Vishnu

4752824




4752824












  • What format do you need the differences to be in?
    – Oded
    Mar 1 '11 at 12:16










  • If you need differnce in data, than yes you need. If you just need to know if files are different than no
    – Stecya
    Mar 1 '11 at 12:16






  • 1




    Read both csv file into list and then apply Linq set operation
    – santosh singh
    Mar 1 '11 at 12:16










  • without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
    – Terry
    Mar 1 '11 at 12:17










  • You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
    – Security Hound
    Mar 1 '11 at 12:17




















  • What format do you need the differences to be in?
    – Oded
    Mar 1 '11 at 12:16










  • If you need differnce in data, than yes you need. If you just need to know if files are different than no
    – Stecya
    Mar 1 '11 at 12:16






  • 1




    Read both csv file into list and then apply Linq set operation
    – santosh singh
    Mar 1 '11 at 12:16










  • without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
    – Terry
    Mar 1 '11 at 12:17










  • You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
    – Security Hound
    Mar 1 '11 at 12:17


















What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16




What format do you need the differences to be in?
– Oded
Mar 1 '11 at 12:16












If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16




If you need differnce in data, than yes you need. If you just need to know if files are different than no
– Stecya
Mar 1 '11 at 12:16




1




1




Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16




Read both csv file into list and then apply Linq set operation
– santosh singh
Mar 1 '11 at 12:16












without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17




without a loop?!? everything runs with loops... reading files such as csv will be done by using reader objects. You read the data by looping through it's lines... so reading the textfile alone will require a loop...
– Terry
Mar 1 '11 at 12:17












You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17






You could read the file and place each line in a collection. However, that would not take into account additions and deletions, it would simply expose the differences. Of course its possible to figure out the differences between files otherwise a program like Winmerge wouldn't exist. It would be not possible to read the entire file, place each line into a collection, without a loop.
– Security Hound
Mar 1 '11 at 12:17














4 Answers
4






active

oldest

votes

















up vote
2
down vote



accepted










Have you looked at the following links ?

If not, then you should.





  • C# - Comparing two CSV Files and giving an output

  • Comparing 2 CSV files in C# advice?






share|improve this answer























  • herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
    – Vishnu
    Mar 2 '11 at 7:13












  • @Anish : Welcome :-)
    – sgokhales
    Mar 2 '11 at 7:47




















up vote
3
down vote













How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream call Getbytes and then pass the two arrays into Enumerable.SequenceEqual



private static byte GetFileHash(string filename)
{
using(var stream = new FileStream(filename, FileMode.Open))
{
var md5Hasher = new MD5CryptoServiceProvider();
return md5Hasher.ComputeHash(stream);
}
}

var file1hash = GetFileHash("file1.ext");
var file2hash = GetFileHash("file2.ext");

var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);


Now there are loops being used, just not by you.






share|improve this answer




























    up vote
    0
    down vote













    No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?



    Anyway, assuming both CSV are sorted by an ID column:




    • Try splitting the files into rows

    • In a loop

      • Split each row as List<string> or as array

      • Compare the lists of both files (ignore trailing empty columns etc.)

      • When differences in data columns were found Save a new row containing the differences into a List<List<string>>

      • When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file








    share|improve this answer






























      up vote
      0
      down vote













      Check the code below to CompareTwoCSVFile and report in another .csv file



      class CompareTwoCSVFile
      {

      public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
      {
      var csv = new StringBuilder();
      string fileContentsOne = File.ReadAllLines(filePathOne);
      string fileContentsTwo = File.ReadAllLines(filePathTwo);

      if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
      return false;

      string columnshead1 = fileContentsOne[0].Split(new char { ';' });
      List<string> heading1 = new List<string>();
      Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
      Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
      string headingsplit = columnshead1[0].Split(',');
      for (int i=0;i< headingsplit.Length;i++)
      {
      heading1.Add(headingsplit[i]);
      }

      var newLine = "";
      newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
      csv.AppendLine(newLine);

      for (int i = 0; i < fileContentsOne.Length-1; ++i)
      {
      string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
      string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });

      string cellOne = columnsOne[0].Split(',');
      string cellTwo = columnsTwo[0].Split(',');
      dict1[i] = new Dictionary<string, string>();
      dict2[i] = new Dictionary<string, string>();
      for(int j=0;j< headingsplit.Length;j++)
      {
      dict1[i].Add(heading1[j],cellOne[j]);
      }
      for (int j = 0; j < headingsplit.Length; j++)
      {
      dict2[i].Add(heading1[j], cellTwo[j]);
      }
      foreach (KeyValuePair<string, string> entry in dict1[i])
      {
      if (dict2[i][entry.Key].Equals(entry.Value)!=true)
      {
      Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
      newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
      csv.AppendLine(newLine);
      }
      }
      }
      File.WriteAllText("D:\Errorlist.csv", csv.ToString());
      return true;
      }


      }






      share|improve this answer










      New contributor




      Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5154370%2fcompare-two-csv-file-using-in-c-sharp%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        Have you looked at the following links ?

        If not, then you should.





        • C# - Comparing two CSV Files and giving an output

        • Comparing 2 CSV files in C# advice?






        share|improve this answer























        • herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
          – Vishnu
          Mar 2 '11 at 7:13












        • @Anish : Welcome :-)
          – sgokhales
          Mar 2 '11 at 7:47

















        up vote
        2
        down vote



        accepted










        Have you looked at the following links ?

        If not, then you should.





        • C# - Comparing two CSV Files and giving an output

        • Comparing 2 CSV files in C# advice?






        share|improve this answer























        • herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
          – Vishnu
          Mar 2 '11 at 7:13












        • @Anish : Welcome :-)
          – sgokhales
          Mar 2 '11 at 7:47















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Have you looked at the following links ?

        If not, then you should.





        • C# - Comparing two CSV Files and giving an output

        • Comparing 2 CSV files in C# advice?






        share|improve this answer














        Have you looked at the following links ?

        If not, then you should.





        • C# - Comparing two CSV Files and giving an output

        • Comparing 2 CSV files in C# advice?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 12:07









        Community

        11




        11










        answered Mar 1 '11 at 12:27









        sgokhales

        35k26105138




        35k26105138












        • herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
          – Vishnu
          Mar 2 '11 at 7:13












        • @Anish : Welcome :-)
          – sgokhales
          Mar 2 '11 at 7:47




















        • herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
          – Vishnu
          Mar 2 '11 at 7:13












        • @Anish : Welcome :-)
          – sgokhales
          Mar 2 '11 at 7:47


















        herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
        – Vishnu
        Mar 2 '11 at 7:13






        herbalessence- i tried with the linq method that given in the above link. but for a csv having 50000 records it taking 30 seconds to return the result. So i tried with hashset that given in the second link and it taking only 2 300 milliseconds to 2second. Thanks
        – Vishnu
        Mar 2 '11 at 7:13














        @Anish : Welcome :-)
        – sgokhales
        Mar 2 '11 at 7:47






        @Anish : Welcome :-)
        – sgokhales
        Mar 2 '11 at 7:47














        up vote
        3
        down vote













        How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream call Getbytes and then pass the two arrays into Enumerable.SequenceEqual



        private static byte GetFileHash(string filename)
        {
        using(var stream = new FileStream(filename, FileMode.Open))
        {
        var md5Hasher = new MD5CryptoServiceProvider();
        return md5Hasher.ComputeHash(stream);
        }
        }

        var file1hash = GetFileHash("file1.ext");
        var file2hash = GetFileHash("file2.ext");

        var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);


        Now there are loops being used, just not by you.






        share|improve this answer

























          up vote
          3
          down vote













          How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream call Getbytes and then pass the two arrays into Enumerable.SequenceEqual



          private static byte GetFileHash(string filename)
          {
          using(var stream = new FileStream(filename, FileMode.Open))
          {
          var md5Hasher = new MD5CryptoServiceProvider();
          return md5Hasher.ComputeHash(stream);
          }
          }

          var file1hash = GetFileHash("file1.ext");
          var file2hash = GetFileHash("file2.ext");

          var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);


          Now there are loops being used, just not by you.






          share|improve this answer























            up vote
            3
            down vote










            up vote
            3
            down vote









            How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream call Getbytes and then pass the two arrays into Enumerable.SequenceEqual



            private static byte GetFileHash(string filename)
            {
            using(var stream = new FileStream(filename, FileMode.Open))
            {
            var md5Hasher = new MD5CryptoServiceProvider();
            return md5Hasher.ComputeHash(stream);
            }
            }

            var file1hash = GetFileHash("file1.ext");
            var file2hash = GetFileHash("file2.ext");

            var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);


            Now there are loops being used, just not by you.






            share|improve this answer












            How much information do you need about the differences? If all you need is the fact that they are different and the requirement to have no loops is fixed you could try taking an MD5 hash and comparing the two hashes. If you don't care about memory usage you could just dump the whole stream into a MemoryStream call Getbytes and then pass the two arrays into Enumerable.SequenceEqual



            private static byte GetFileHash(string filename)
            {
            using(var stream = new FileStream(filename, FileMode.Open))
            {
            var md5Hasher = new MD5CryptoServiceProvider();
            return md5Hasher.ComputeHash(stream);
            }
            }

            var file1hash = GetFileHash("file1.ext");
            var file2hash = GetFileHash("file2.ext");

            var areEqual = Enumerable.SequenceEqual(file1hash, file2hash);


            Now there are loops being used, just not by you.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 1 '11 at 12:40









            ilivewithian

            12.1k1676149




            12.1k1676149






















                up vote
                0
                down vote













                No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?



                Anyway, assuming both CSV are sorted by an ID column:




                • Try splitting the files into rows

                • In a loop

                  • Split each row as List<string> or as array

                  • Compare the lists of both files (ignore trailing empty columns etc.)

                  • When differences in data columns were found Save a new row containing the differences into a List<List<string>>

                  • When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file








                share|improve this answer



























                  up vote
                  0
                  down vote













                  No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?



                  Anyway, assuming both CSV are sorted by an ID column:




                  • Try splitting the files into rows

                  • In a loop

                    • Split each row as List<string> or as array

                    • Compare the lists of both files (ignore trailing empty columns etc.)

                    • When differences in data columns were found Save a new row containing the differences into a List<List<string>>

                    • When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file








                  share|improve this answer

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?



                    Anyway, assuming both CSV are sorted by an ID column:




                    • Try splitting the files into rows

                    • In a loop

                      • Split each row as List<string> or as array

                      • Compare the lists of both files (ignore trailing empty columns etc.)

                      • When differences in data columns were found Save a new row containing the differences into a List<List<string>>

                      • When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file








                    share|improve this answer














                    No, there is no way without using loops. How do you expect any compare algorithm to iterate over the characters / words / tokens / lines of the file without using loops?



                    Anyway, assuming both CSV are sorted by an ID column:




                    • Try splitting the files into rows

                    • In a loop

                      • Split each row as List<string> or as array

                      • Compare the lists of both files (ignore trailing empty columns etc.)

                      • When differences in data columns were found Save a new row containing the differences into a List<List<string>>

                      • When different IDs were found, compare both ID: Save the row with the smaller ID (which identifies the additional row) and get the next row of this file









                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 1 '11 at 12:27

























                    answered Mar 1 '11 at 12:20









                    matthias.lukaszek

                    1,5671729




                    1,5671729






















                        up vote
                        0
                        down vote













                        Check the code below to CompareTwoCSVFile and report in another .csv file



                        class CompareTwoCSVFile
                        {

                        public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
                        {
                        var csv = new StringBuilder();
                        string fileContentsOne = File.ReadAllLines(filePathOne);
                        string fileContentsTwo = File.ReadAllLines(filePathTwo);

                        if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
                        return false;

                        string columnshead1 = fileContentsOne[0].Split(new char { ';' });
                        List<string> heading1 = new List<string>();
                        Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
                        Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
                        string headingsplit = columnshead1[0].Split(',');
                        for (int i=0;i< headingsplit.Length;i++)
                        {
                        heading1.Add(headingsplit[i]);
                        }

                        var newLine = "";
                        newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
                        csv.AppendLine(newLine);

                        for (int i = 0; i < fileContentsOne.Length-1; ++i)
                        {
                        string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
                        string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });

                        string cellOne = columnsOne[0].Split(',');
                        string cellTwo = columnsTwo[0].Split(',');
                        dict1[i] = new Dictionary<string, string>();
                        dict2[i] = new Dictionary<string, string>();
                        for(int j=0;j< headingsplit.Length;j++)
                        {
                        dict1[i].Add(heading1[j],cellOne[j]);
                        }
                        for (int j = 0; j < headingsplit.Length; j++)
                        {
                        dict2[i].Add(heading1[j], cellTwo[j]);
                        }
                        foreach (KeyValuePair<string, string> entry in dict1[i])
                        {
                        if (dict2[i][entry.Key].Equals(entry.Value)!=true)
                        {
                        Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
                        newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
                        csv.AppendLine(newLine);
                        }
                        }
                        }
                        File.WriteAllText("D:\Errorlist.csv", csv.ToString());
                        return true;
                        }


                        }






                        share|improve this answer










                        New contributor




                        Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






















                          up vote
                          0
                          down vote













                          Check the code below to CompareTwoCSVFile and report in another .csv file



                          class CompareTwoCSVFile
                          {

                          public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
                          {
                          var csv = new StringBuilder();
                          string fileContentsOne = File.ReadAllLines(filePathOne);
                          string fileContentsTwo = File.ReadAllLines(filePathTwo);

                          if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
                          return false;

                          string columnshead1 = fileContentsOne[0].Split(new char { ';' });
                          List<string> heading1 = new List<string>();
                          Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
                          Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
                          string headingsplit = columnshead1[0].Split(',');
                          for (int i=0;i< headingsplit.Length;i++)
                          {
                          heading1.Add(headingsplit[i]);
                          }

                          var newLine = "";
                          newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
                          csv.AppendLine(newLine);

                          for (int i = 0; i < fileContentsOne.Length-1; ++i)
                          {
                          string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
                          string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });

                          string cellOne = columnsOne[0].Split(',');
                          string cellTwo = columnsTwo[0].Split(',');
                          dict1[i] = new Dictionary<string, string>();
                          dict2[i] = new Dictionary<string, string>();
                          for(int j=0;j< headingsplit.Length;j++)
                          {
                          dict1[i].Add(heading1[j],cellOne[j]);
                          }
                          for (int j = 0; j < headingsplit.Length; j++)
                          {
                          dict2[i].Add(heading1[j], cellTwo[j]);
                          }
                          foreach (KeyValuePair<string, string> entry in dict1[i])
                          {
                          if (dict2[i][entry.Key].Equals(entry.Value)!=true)
                          {
                          Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
                          newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
                          csv.AppendLine(newLine);
                          }
                          }
                          }
                          File.WriteAllText("D:\Errorlist.csv", csv.ToString());
                          return true;
                          }


                          }






                          share|improve this answer










                          New contributor




                          Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Check the code below to CompareTwoCSVFile and report in another .csv file



                            class CompareTwoCSVFile
                            {

                            public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
                            {
                            var csv = new StringBuilder();
                            string fileContentsOne = File.ReadAllLines(filePathOne);
                            string fileContentsTwo = File.ReadAllLines(filePathTwo);

                            if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
                            return false;

                            string columnshead1 = fileContentsOne[0].Split(new char { ';' });
                            List<string> heading1 = new List<string>();
                            Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
                            Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
                            string headingsplit = columnshead1[0].Split(',');
                            for (int i=0;i< headingsplit.Length;i++)
                            {
                            heading1.Add(headingsplit[i]);
                            }

                            var newLine = "";
                            newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
                            csv.AppendLine(newLine);

                            for (int i = 0; i < fileContentsOne.Length-1; ++i)
                            {
                            string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
                            string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });

                            string cellOne = columnsOne[0].Split(',');
                            string cellTwo = columnsTwo[0].Split(',');
                            dict1[i] = new Dictionary<string, string>();
                            dict2[i] = new Dictionary<string, string>();
                            for(int j=0;j< headingsplit.Length;j++)
                            {
                            dict1[i].Add(heading1[j],cellOne[j]);
                            }
                            for (int j = 0; j < headingsplit.Length; j++)
                            {
                            dict2[i].Add(heading1[j], cellTwo[j]);
                            }
                            foreach (KeyValuePair<string, string> entry in dict1[i])
                            {
                            if (dict2[i][entry.Key].Equals(entry.Value)!=true)
                            {
                            Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
                            newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
                            csv.AppendLine(newLine);
                            }
                            }
                            }
                            File.WriteAllText("D:\Errorlist.csv", csv.ToString());
                            return true;
                            }


                            }






                            share|improve this answer










                            New contributor




                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            Check the code below to CompareTwoCSVFile and report in another .csv file



                            class CompareTwoCSVFile
                            {

                            public bool ReportErrorOnCompareCSV(string filePathOne, string filePathTwo)
                            {
                            var csv = new StringBuilder();
                            string fileContentsOne = File.ReadAllLines(filePathOne);
                            string fileContentsTwo = File.ReadAllLines(filePathTwo);

                            if (!fileContentsOne.Length.Equals(fileContentsTwo.Length))
                            return false;

                            string columnshead1 = fileContentsOne[0].Split(new char { ';' });
                            List<string> heading1 = new List<string>();
                            Dictionary<string, string> dict1 = new Dictionary<string, string>[fileContentsOne.Length];
                            Dictionary<string, string> dict2 = new Dictionary<string, string>[fileContentsTwo.Length];
                            string headingsplit = columnshead1[0].Split(',');
                            for (int i=0;i< headingsplit.Length;i++)
                            {
                            heading1.Add(headingsplit[i]);
                            }

                            var newLine = "";
                            newLine = string.Format("{0},{1},{2},{3}", "File1_ColumnName", "File1_ColumnValue", "File2_ColumnName", "File2_ColumnValue");
                            csv.AppendLine(newLine);

                            for (int i = 0; i < fileContentsOne.Length-1; ++i)
                            {
                            string columnsOne = fileContentsOne[i+1].Split(new char { ';' });
                            string columnsTwo = fileContentsTwo[i+1].Split(new char { ';' });

                            string cellOne = columnsOne[0].Split(',');
                            string cellTwo = columnsTwo[0].Split(',');
                            dict1[i] = new Dictionary<string, string>();
                            dict2[i] = new Dictionary<string, string>();
                            for(int j=0;j< headingsplit.Length;j++)
                            {
                            dict1[i].Add(heading1[j],cellOne[j]);
                            }
                            for (int j = 0; j < headingsplit.Length; j++)
                            {
                            dict2[i].Add(heading1[j], cellTwo[j]);
                            }
                            foreach (KeyValuePair<string, string> entry in dict1[i])
                            {
                            if (dict2[i][entry.Key].Equals(entry.Value)!=true)
                            {
                            Console.WriteLine("Mismatch Values on row "+i+":n File1 "+entry.Key + "-" + entry.Value+"n File2 "+entry.Key+"-"+ dict2[i][entry.Key]);
                            newLine = string.Format("{0},{1},{2},{3}", entry.Key, entry.Value, entry.Key, dict2[i][entry.Key]);
                            csv.AppendLine(newLine);
                            }
                            }
                            }
                            File.WriteAllText("D:\Errorlist.csv", csv.ToString());
                            return true;
                            }


                            }







                            share|improve this answer










                            New contributor




                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer








                            edited Nov 22 at 5:06





















                            New contributor




                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered Nov 21 at 3:10









                            Naveen_mike

                            12




                            12




                            New contributor




                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Naveen_mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5154370%2fcompare-two-csv-file-using-in-c-sharp%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Berounka

                                Sphinx de Gizeh

                                Different font size/position of beamer's navigation symbols template's content depending on regular/plain...