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
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 AutomaticReplace 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"
# Start the service
sc.exe start "FileSystemWatcher"
# Or using PowerShell
Start-Service -Name "FileSystemWatcher"# Check status
sc.exe query "FileSystemWatcher"
# Or using PowerShell
Get-Service -Name "FileSystemWatcher"# Stop the service
sc.exe stop "FileSystemWatcher"
# Or using PowerShell
Stop-Service -Name "FileSystemWatcher"# Stop first if running
sc.exe stop "FileSystemWatcher"
# Delete the service
sc.exe delete "FileSystemWatcher"
# Or using PowerShell
Remove-Service -Name "FileSystemWatcher"For testing purposes, you can run the application in console mode (it auto-detects):
.\FileSystemWatcherMerged.exeThis will run the same logic but in a console window. Press Ctrl+C to stop.
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 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 changesBackupPath: Directory where backups are storedPollIntervalSeconds: How often to check for queued changes (default: 1 second)ExcludedFolders: Folders to ignore during monitoringExcludedExtensions: File extensions to ignore
- Service Starts → Windows Service Manager launches the executable
- Initialization → Loads configuration from appsettings.json
- File Monitoring → FileSystemWatcher monitors source directory
- Change Detection → Files changes are queued for backup
- Scheduled Backup → Every PollIntervalSeconds, processes queued changes
- Graceful Shutdown → Service stops cleanly when requested
For a Windows Server environment:
-
Build in Release mode:
dotnet build -c Release -
Merge to single EXE (already done):
FileSystemWatcherMerged.execontains all dependencies
-
Install Service (as Administrator):
sc.exe create "FileSystemWatcher" binPath= "C:\Production\Path\FileSystemWatcherMerged.exe" start= auto sc.exe start "FileSystemWatcher"
-
Verify Installation:
sc.exe query "FileSystemWatcher" Get-EventLog -LogName System -Source FileSystemWatcher -Newest 10
-
Monitor Logs via Windows Event Viewer or application logs
- ✅ 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
- ✅ Run in console mode first to check for configuration errors
- ✅ Verify appsettings.json is present and valid JSON
- ✅ Check paths in configuration exist
- ✅ 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
- ✅ Increase PollIntervalSeconds in configuration
- ✅ Check for large number of file changes
- ✅ Verify excluded folders/extensions are configured correctly
- 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