In my previous article – File Comparison in C# – Part 1 – I showed you how to create a simple method which compares the file size of two files using the System.IO.FileInfo
class. In this article, we will be building upon that previous article so I suggest you read it here before continuing.
Now we are going to create a file comparison method which will compare files byte by byte. To do this we are going to use the System.IO.FileStream
class to read our files. The below code is one way of implementing this comparison technique:
private static bool CompareFileBytes(string fileName1, string fileName2)
{
// Compare file sizes before continuing.
// If sizes are equal then compare bytes.
if (CompareFileSizes(fileName1, fileName2))
{
int file1byte = 0;
int file2byte = 0;
// Open a System.IO.FileStream for each file.
// Note: With the 'using' keyword the streams
// are closed automatically.
using (FileStream fileStream1 = new FileStream(fileName1, FileMode.Open),
fileStream2 = new FileStream(fileName2, FileMode.Open))
{
// Read and compare a byte from each file until a
// non-matching set of bytes is found or the end of
// file is reached.
do
{
file1byte = fileStream1.ReadByte();
file2byte = fileStream2.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
}
return ((file1byte - file2byte) == 0);
}
else
{
return false;
}
}
[continue reading…]
As a developer you might need to compare files for equality, maybe because you want to verify that your backups are valid, or maybe because you want to search for duplicate files in a folder. Either way you will need to compare two files together, and I am going to show you how.
There are a number of different ways to compare files – some are good and some are not so good. Below is a list of some of the different techniques that can be used, either on their own, or together, to compare files.
- Compare file sizes;
- Compare file creation dates;
- Compare bytes from beginning and end of files;
- Compare whole file byte by byte;
- Generate a hash for each file and compare hashes;
All these comparison techniques have advantages and disadvantages. For example technique number 1 has the advantage of being very fast to execute, but then it is very unreliable because the chances of having files with the same size can be quite high. Technique number 3 for example, is very accurate but can be slow for large files. There is no one comparison technique which is good for all situations, so I recommend using multiple techniques in sequence.
[continue reading…]
A few days ago I wrote an article on basic XML serialization which looked at the XML serialization of an Employee
object that we created. In this article I am going to build on that previous article by serializing a collection of Employee
objects, so if you have not read Basic XML Serialization in C# I suggest you do that before reading this article.
Sometimes a developer would need to serialize a collection of objects such as a List
or an ArrayList
. In this article I am going to show you how to serialize a List
of Employee
objects to an XML file.
We are going to use the Employee
class we created in the Basic XML Serialization in C# article. Then we are going to create another class which will be the collection class for our Employee
class. We can call it EmployeeList
. Below is what this class should look like:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace XMLSerialization
{
[XmlRoot("CompanyEmployees")]
public class EmployeeList
{
[XmlArray("EmployeeListing")]
[XmlArrayItem("Employee", typeof(Employee))]
public List employeeList;
// Constructor
public EmployeeList()
{
employeeList = new List();
}
public void AddEmployee(Employee employee)
{
employeeList.Add(employee);
}
}
}
[continue reading…]
A regular expression, or regex in short, is a string of text which represents a search pattern. They are usually used either to search through text and find strings which match the given pattern, or to validate a string against the search pattern.
Regular expressions are extremely powerful but they can prove quite difficult to understand and get the hang of. Once you do manage to get the hang of them, you will most likely find that they are great to use in your code and they can save you plenty of coding time.
In this article I will be showing you how to validate an IP v4 address using a regular expression. The .NET framework contains a class called System.Text.RegularExpressions.Regex
which is just what we need to validate a string of text – in our case the IP v4 address.
[continue reading…]
Serialization is the process of converting the state of an object into a sequence of bits so that it can be transferred over a network or saved to a file on disk. With XML Serialization, instead of converting an object’s state to bits, it will convert an object’s state to XML. This is particularly useful for saving and loading application configurations for example.
In this short article we will be creating an employee class and saving an instance of the employee class to an xml file on disk through XML serialization. We will then attempt to load back the employee object from the XML file we created though XML deserialization.
So first let’s create a class called Employee. We are going to add five properties to our Employee
class – Name, Surname, DateOfBirth, Sex, and Position. Obviously, an actual employee class would have much more properties such as addresses, contact details, social security number, id number, etc, but for the purpose of this example five properties are more than enough.
Below is what our basic Employee
class looks like:
using System;
namespace XMLSerialization
{
public class Employee
{
public enum EmployeeSex
{
Male,
Female
}
// Private Members
private string name;
private string surname;
private DateTime dob;
private EmployeeSex sex;
private string position;
// Public Properties
public string Name
{
get { return name; }
set { name = value; }
}
public string Surname
{
get { return surname; }
set { surname = value; }
}
public DateTime DateOfBirth
{
get { return dob; }
set { dob = value; }
}
public EmployeeSex Sex
{
get { return sex; }
set { sex = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
// Constructor
public Employee()
{
}
}
}
[continue reading…]