---
title: "How do you compare last modified time between two versions of a file?"  
description: "How do you compare last modified time between two versions of a file?"  
author: "Ravi Vishwakarma"  
published: 2025-05-15  
updated: 2025-05-15  
canonical: https://www.mindstick.com/interview/34125/how-do-you-compare-last-modified-time-between-two-versions-of-a-file  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you compare last modified time between two versions of a file?

To **compare the last modified time between two versions of a file** in C#, you can use `File.GetLastWriteTime()` or `FileInfo.LastWriteTime`.

### Example Using `File.GetLastWriteTime`

```cs
using System;
using System.IO;

class FileComparer
{
    public static void CompareFileModificationTimes(string filePath1, string filePath2)
    {
        DateTime file1Time = File.GetLastWriteTime(filePath1);
        DateTime file2Time = File.GetLastWriteTime(filePath2);

        Console.WriteLine($"File 1: {file1Time}");
        Console.WriteLine($"File 2: {file2Time}");

        if (file1Time > file2Time)
            Console.WriteLine("File 1 is newer.");
        else if (file1Time < file2Time)
            Console.WriteLine("File 2 is newer.");
        else
            Console.WriteLine("Both files were modified at the same time.");
    }
}
```

### Usage

```cs
FileComparer.CompareFileModificationTimes("version1.txt", "version2.txt");
```

### Tips

1. Use `FileInfo.LastWriteTimeUtc` for comparing across time zones.
2. You can compare `CreationTime` instead if needed.
3. Always check that both files exist before comparing to avoid exceptions.

## Read More

1. [How do you prevent a file from being modified while reading it?](https://www.mindstick.com/interview/34122/how-do-you-prevent-a-file-from-being-modified-while-reading-it)
2. [How can you determine the creation time or last modified time of a file?](https://www.mindstick.com/interview/34093/how-can-you-determine-the-creation-time-or-last-modified-time-of-a-file)
3. [How to create a Cookie in C#, which is both side modifiable.](https://www.mindstick.com/forum/161568/how-to-create-a-cookie-in-c-sharp-which-is-both-side-modifiable)

## Answers

### Answer by Ravi Vishwakarma

To **compare the last modified time between two versions of a file** in C#, you can use `File.GetLastWriteTime()` or `FileInfo.LastWriteTime`.

### Example Using `File.GetLastWriteTime`

```cs
using System;
using System.IO;

class FileComparer
{
    public static void CompareFileModificationTimes(string filePath1, string filePath2)
    {
        DateTime file1Time = File.GetLastWriteTime(filePath1);
        DateTime file2Time = File.GetLastWriteTime(filePath2);

        Console.WriteLine($"File 1: {file1Time}");
        Console.WriteLine($"File 2: {file2Time}");

        if (file1Time > file2Time)
            Console.WriteLine("File 1 is newer.");
        else if (file1Time < file2Time)
            Console.WriteLine("File 2 is newer.");
        else
            Console.WriteLine("Both files were modified at the same time.");
    }
}
```

### Usage

```cs
FileComparer.CompareFileModificationTimes("version1.txt", "version2.txt");
```

### Tips

1. Use `FileInfo.LastWriteTimeUtc` for comparing across time zones.
2. You can compare `CreationTime` instead if needed.
3. Always check that both files exist before comparing to avoid exceptions.

## Read More

1. [How do you prevent a file from being modified while reading it?](https://www.mindstick.com/interview/34122/how-do-you-prevent-a-file-from-being-modified-while-reading-it)
2. [How can you determine the creation time or last modified time of a file?](https://www.mindstick.com/interview/34093/how-can-you-determine-the-creation-time-or-last-modified-time-of-a-file)
3. [How to create a Cookie in C#, which is both side modifiable.](https://www.mindstick.com/forum/161568/how-to-create-a-cookie-in-c-sharp-which-is-both-side-modifiable)


---

Original Source: https://www.mindstick.com/interview/34125/how-do-you-compare-last-modified-time-between-two-versions-of-a-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
