Skip to content

dipeshchaudhari3255/FileSystemWatcher

Repository files navigation

FileSystemWatcher - Windows Service Setup Guide

📋 Overview

This Windows Service monitors a source directory and automatically backs it up. It includes:

  • ✅ Automatic startup on Windows boot
  • ✅ Runs in background as a Windows Service
  • ✅ Graceful shutdown mechanism
  • ✅ Event-driven file monitoring with scheduled backups

🚀 How to Use

1. Install the Windows Service

Run PowerShell as Administrator and execute:

# Install the service
sc.exe create "FileSystemWatcher" binPath= "C:\Path\To\FileSystemWatcherMerged.exe" start= auto

# Or using PowerShell
New-Service -Name "FileSystemWatcher" -BinaryPathName "C:\Path\To\FileSystemWatcherMerged.exe" -StartupType Automatic

Replace C:\Path\To\FileSystemWatcherMerged.exe with your actual executable path.

What it does:

  • Registers the application as a Windows Service
  • Sets it to start automatically on Windows boot
  • Service name: "FileSystemWatcher"

2. Start the Service

# Start the service
sc.exe start "FileSystemWatcher"

# Or using PowerShell
Start-Service -Name "FileSystemWatcher"

3. Check Service Status

# Check status
sc.exe query "FileSystemWatcher"

# Or using PowerShell
Get-Service -Name "FileSystemWatcher"

4. Stop the Service

# Stop the service
sc.exe stop "FileSystemWatcher"

# Or using PowerShell
Stop-Service -Name "FileSystemWatcher"

5. Uninstall the Service

# Stop first if running
sc.exe stop "FileSystemWatcher"

# Delete the service
sc.exe delete "FileSystemWatcher"

# Or using PowerShell
Remove-Service -Name "FileSystemWatcher"

🧪 Testing in Console Mode

For testing purposes, you can run the application in console mode (it auto-detects):

.\FileSystemWatcherMerged.exe

This will run the same logic but in a console window. Press Ctrl+C to stop.


📁 File Structure

FileSystemWatcher/
├── Program.cs                 # Main application (service/console detection)
├── FolderBackupWorker.cs      # Service implementation
├── FileSystemWatcher.csproj   # Project file
├── appsettings.json           # Configuration
├── FileSystemWatcherMerged.exe # Single merged executable
└── README.md                 # This file

🔧 Configuration

Configuration lives in appsettings.json next to the executable:

{
   "FileSystemWatcher": {
      "SourcePath": "D:\\DC\\Project",
      "BackupPath": "D:\\DC\\Backup\\DailyBackup",
      "PollIntervalSeconds": 1,
      "ExcludedFolders": [ "bin", "obj", ".vs" ],
      "ExcludedExtensions": [ ".user" ]
   }
}

Configuration Options:

  • SourcePath: Directory to monitor for changes
  • BackupPath: Directory where backups are stored
  • PollIntervalSeconds: How often to check for queued changes (default: 1 second)
  • ExcludedFolders: Folders to ignore during monitoring
  • ExcludedExtensions: File extensions to ignore

📊 How It Works

  1. Service Starts → Windows Service Manager launches the executable
  2. Initialization → Loads configuration from appsettings.json
  3. File Monitoring → FileSystemWatcher monitors source directory
  4. Change Detection → Files changes are queued for backup
  5. Scheduled Backup → Every PollIntervalSeconds, processes queued changes
  6. Graceful Shutdown → Service stops cleanly when requested

🎯 Recommended Production Setup

For a Windows Server environment:

  1. Build in Release mode:

    dotnet build -c Release
  2. Merge to single EXE (already done):

    • FileSystemWatcherMerged.exe contains all dependencies
  3. Install Service (as Administrator):

    sc.exe create "FileSystemWatcher" binPath= "C:\Production\Path\FileSystemWatcherMerged.exe" start= auto
    sc.exe start "FileSystemWatcher"
  4. Verify Installation:

    sc.exe query "FileSystemWatcher"
    Get-EventLog -LogName System -Source FileSystemWatcher -Newest 10
  5. Monitor Logs via Windows Event Viewer or application logs


🐛 Troubleshooting

Service won't start

  • ✅ Verify executable path is correct in service configuration
  • ✅ Check file permissions - service runs under Local System by default
  • ✅ Ensure source and backup directories exist and are accessible
  • ✅ Check Windows Event Viewer for error details

Service starts but stops immediately

  • ✅ Run in console mode first to check for configuration errors
  • ✅ Verify appsettings.json is present and valid JSON
  • ✅ Check paths in configuration exist

Files not being backed up

  • ✅ Verify source directory has files and write permissions
  • ✅ Check backup directory has write permissions
  • ✅ Review service logs in Event Viewer
  • ✅ Test with console mode to see live logging

High CPU usage

  • ✅ Increase PollIntervalSeconds in configuration
  • ✅ Check for large number of file changes
  • ✅ Verify excluded folders/extensions are configured correctly

📝 Notes

  • The service runs under Local System account by default
  • File monitoring is event-driven, backup processing is scheduled
  • Excludes: bin, obj, .vs folders and *.user files by default
  • Backup operations copy entire directory structure
  • Service logs to Windows Event Log and console (when in console mode)
  • Configuration changes require service restart to take effect

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors