All Resources
Article.NET2026-03-24

File Sync with Conflict Detection

Implement a manifest-based file synchronization system with conflict detection. Track file states via JSON, compare timestamps with tolerance, and detect when files were modified by others during offline work.

The Problem

Engineers cache files locally, edit them offline, then need to sync back to the server. The challenge: what if someone else modified the same file on the server while you were working offline?

Without conflict detection, you get silent overwrites — the last person to save wins, and the other's work is lost. This is especially dangerous with SolidWorks assemblies where multiple engineers edit referenced parts.

Architecture: The Manifest Approach

Instead of a database, store a JSON manifest alongside the cached files:

{
    "version": 1,
    "files": {
        "C:/Server/Part1.SLDPRT": {
            "originalLastModified": "2026-03-10T14:30:00",
            "originalSize": 245760,
            "isLibraryFile": false
        }
    }
}

The manifest records the original state of each file at the time of download. This enables three-way comparison.

Change Detection Algorithm

For each cached file, compare three states:

StateSourceMeaning
Manifest originalJSON fileWhat the file looked like when downloaded
Current localLocal diskWhat the file looks like now (after user edits)
Current serverServer diskWhat the file looks like on the server now

Decision Matrix

Local Changed?Server Changed?Action
NoNoSkip — file is unchanged
YesNoSafe to upload — only local edits
NoYesSafe to download — only server edits
YesYes**Conflict** — both sides modified

Timestamp Comparison with Tolerance

Detecting change requires comparing timestamps — but file system precision varies:

bool HasChanged(FileInfo current, DateTime originalTime,
    long originalSize)
{
    // Size change is definitive
    if (current.Length != originalSize) return true;

    // Timestamp with 2-second tolerance
    var diff = Math.Abs(
        (current.LastWriteTime - originalTime).TotalSeconds);
    return diff > 2;
}

The 2-second tolerance handles FAT32 drives, USB sticks, and network shares where timestamps are rounded.

Resolution Strategies

When a conflict is detected, your tool should:

  1. Skip library files — read-only shared files (toolbox components) should never be modified locally
  2. Prompt the user — show both timestamps and sizes, let them choose which version to keep
  3. Keep both — rename the local copy with a .local suffix so neither version is lost

Manifest Best Practices

  • Version field: Include a version number so future code can handle format changes
  • Relative paths: Store paths relative to the cache root so the manifest is portable
  • Atomic writes: Write the manifest to a temp file, then rename — avoids corruption if the process crashes mid-write
  • Exclude patterns: Skip temp files (~$*, *.tmp, .swp) that SolidWorks creates during editing
C#.NETFile SyncJSONConflict Detection