none
how to compare 2 .csv files in C# RRS feed

Answers

All replies

  • I would use oledb to connect to the files and then use a SQL statement to find the differences.

    jdweng

    Monday, June 3, 2013 12:21 PM
  • You could some method like the one below;

    private bool CompareCsvFiles(string filePathOne, string filePathTwo) {
          string[] fileContentsOne = File.ReadAllLines(filePathOne);
          string[] fileContentsTwo = File.ReadAllLines(filePathTwo);
          //Compare length of files
          if(!fileContentsOne.Length.Equals(fileContentsTwo.Length))
            return false;
          for(int i = 0; i < fileContentsOne.Length; ++i)
          {
            string[] columnsOne = fileContentsOne[i].Split(new char[] { ';'});
            string[] columnsTwo = fileContentsOne[i].Split(new char[] { ';' });
            //Compare number of columns on each row
            if (!columnsOne.Length.Equals(columnsTwo.Length))
              return false;
            //Compare column values
            for(int j = 0 j < columnsOne.Length; ++j) {
              if(!columnsOne[j].Equals(columnsTwo[j]))
                return false;
            }
          }
          return true;
        }

    Monday, June 3, 2013 2:56 PM