pascalabcnet/VisualPascalABCNET/FileMonitoring.cs

140 lines
4.1 KiB
C#
Raw Normal View History

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
2015-06-02 22:52:35 +03:00
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
2015-05-14 22:35:07 +03:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace VisualPascalABC
{
public class FileChangeWatcher : IDisposable
{
FileSystemWatcher watcher;
string fileName;
Hashtable activeWatchers = new Hashtable();
bool wasChangedExternally = false;
public FileChangeWatcher(string path)
{
VisualPABCSingleton.MainForm.Activated += MainForm_Activated;
fileName = path;
activeWatchers.Add(this, this);
SetWatcher();
}
public void Dispose()
{
activeWatchers.Remove(this);
VisualPABCSingleton.MainForm.Activated -= MainForm_Activated;
if (watcher != null)
{
watcher.Dispose();
watcher = null;
}
}
bool enabled = true;
public bool Enabled
{
get { return enabled; }
set
{
enabled = value;
SetWatcher();
}
}
void SetWatcher()
{
if (watcher != null)
{
watcher.EnableRaisingEvents = false;
}
if (!enabled)
return;
try
{
if (watcher == null)
{
watcher = new FileSystemWatcher();
watcher.SynchronizingObject = VisualPABCSingleton.MainForm;
watcher.Changed += OnFileChangedEvent;
watcher.Created += OnFileChangedEvent;
watcher.Renamed += OnFileChangedEvent;
}
watcher.Path = Path.GetDirectoryName(fileName);
watcher.Filter = Path.GetFileName(fileName);
watcher.EnableRaisingEvents = true;
}
catch (PlatformNotSupportedException)
{
if (watcher != null)
{
watcher.Dispose();
}
watcher = null;
}
}
void OnFileChangedEvent(object sender, FileSystemEventArgs e)
{
if (!wasChangedExternally)
{
wasChangedExternally = true;
}
}
2022-10-23 13:37:30 +03:00
void showChangedFileMessage()
{
string mes = null;
if (!File.Exists(fileName))
{
mes = Form1StringResources.Get("FILE_NOT_EXIST_MESSAGE");
if (MessageBox.Show(fileName + "\n\n" + mes, Form1StringResources.Get("CHANGED_FILE"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
WorkbenchServiceFactory.FileService.SetFileAsChanged(fileName);
}
else
{
WorkbenchServiceFactory.FileService.CloseFile(fileName);
}
return;
}
mes = Form1StringResources.Get("FILE_CHANGED_MESSAGE");
if (MessageBox.Show(fileName + "\n\n" + mes, Form1StringResources.Get("CHANGED_FILE"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
WorkbenchServiceFactory.FileService.ReloadFile(fileName);
}
else
{
WorkbenchServiceFactory.FileService.SetFileAsChanged(fileName);
}
}
void MainForm_Activated(object sender, EventArgs e)
2015-05-14 22:35:07 +03:00
{
try
{
if (wasChangedExternally)
2015-05-14 22:35:07 +03:00
{
wasChangedExternally = false;
2022-10-23 13:37:30 +03:00
WorkbenchServiceFactory.Workbench.MainForm.BeginInvoke((Action)showChangedFileMessage);
2015-05-14 22:35:07 +03:00
}
}
catch (Exception ex)
{
2015-05-14 22:35:07 +03:00
}
}
2015-05-14 22:35:07 +03:00
}
}