/IO/FileSystemWatcher.cs |
@@ -0,0 +1,73 @@ |
using System; |
using System.IO; |
using System.Runtime.Caching; |
|
namespace wasSharpNET.IO |
{ |
/// <summary> |
/// Filesystem watcher that ensures events are not fired twice |
/// </summary> |
/// <remarks>derived from Ben Hall @ benhall.io</remarks> |
public class FileSystemWatcher : IDisposable |
{ |
public event FileSystemEventHandler FilesytemEvent; |
private readonly CacheItemPolicy _cacheItemPolicy; |
private readonly int _cacheTimeMilliseconds = 1000; |
private readonly MemoryCache _memCache; |
|
public FileSystemWatcher(string path, NotifyFilters notifyFilters, string filter) |
{ |
_memCache = MemoryCache.Default; |
|
var watcher = new System.IO.FileSystemWatcher |
{ |
Path = path, |
NotifyFilter = notifyFilters, |
Filter = filter |
}; |
|
_cacheItemPolicy = new CacheItemPolicy |
{ |
RemovedCallback = OnRemovedFromCache |
}; |
|
watcher.Changed += OnChanged; |
watcher.EnableRaisingEvents = true; |
} |
|
public FileSystemWatcher(string path, NotifyFilters notifyFilters, string filter, int timeout) : this(path, |
notifyFilters, filter) |
{ |
_cacheTimeMilliseconds = timeout; |
} |
|
// Add file event to cache for CacheTimeMilliseconds |
private void OnChanged(object source, FileSystemEventArgs e) |
{ |
_cacheItemPolicy.AbsoluteExpiration = |
DateTimeOffset.Now.AddMilliseconds(_cacheTimeMilliseconds); |
|
// Only add if it is not there already (swallow others) |
_memCache.AddOrGetExisting(e.Name, e, _cacheItemPolicy); |
} |
|
// Handle cache item expiring |
private void OnRemovedFromCache(CacheEntryRemovedArguments args) |
{ |
if (args.RemovedReason != CacheEntryRemovedReason.Expired) return; |
|
// Now actually handle file event. |
OnFileSystemEvent((FileSystemEventArgs)args.CacheItem.Value); |
} |
|
protected virtual void OnFileSystemEvent(FileSystemEventArgs e) |
{ |
FilesytemEvent?.Invoke(this, e); |
} |
|
public void Dispose() |
{ |
_memCache.Dispose(); |
} |
} |
} |
/Platform/Windows/Commands/NetSH/URLACL.cs |
@@ -4,6 +4,7 @@ |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.ComponentModel; |
using System.Diagnostics; |
using System.Text; |
@@ -11,21 +12,20 @@ |
|
namespace wasSharpNET.Platform.Windows.Commands.NetSH |
{ |
public class URLACL |
public class URLACL : IDisposable |
{ |
private readonly string domain; |
private readonly int timeout; |
private readonly string URL; |
private string domain { get; set; } |
private int timeout { get; set; } = 60000; |
private string URL { get; set; } |
|
private readonly Regex URLReservationRegex; |
private readonly string username; |
private Regex URLReservationRegex { get; set; } |
private string username { get; set; } |
|
public URLACL(string URL, string username, string domain, int timeout) |
public URLACL(string URL, string username, string domain) |
{ |
this.URL = URL; |
this.username = username; |
this.domain = domain; |
this.timeout = timeout; |
|
URLReservationRegex = |
new Regex( |
@@ -34,8 +34,13 @@ |
RegexOptions.Compiled); |
} |
|
public bool isReserved |
public URLACL(string URL, string username, string domain, int timeout) : this(URL, username, domain) |
{ |
this.timeout = timeout; |
} |
|
public bool IsReserved |
{ |
get |
{ |
var netSHOutput = new StringBuilder(); |
@@ -58,7 +63,7 @@ |
|
checkProcess.Start(); |
checkProcess.BeginOutputReadLine(); |
checkProcess.WaitForExit(60000); |
checkProcess.WaitForExit(timeout); |
|
return URLReservationRegex.IsMatch(netSHOutput.ToString()); |
} |
@@ -117,5 +122,9 @@ |
|
return process.WaitForExit(timeout); |
} |
|
public void Dispose() |
{ |
} |
} |
} |
/wasSharpNET.csproj |
@@ -32,6 +32,7 @@ |
<ItemGroup> |
<Reference Include="System" /> |
<Reference Include="System.Core" /> |
<Reference Include="System.Runtime.Caching" /> |
<Reference Include="System.Security" /> |
<Reference Include="System.ServiceModel" /> |
<Reference Include="System.ServiceProcess" /> |
@@ -48,6 +49,7 @@ |
<Compile Include="Cryptography\AES.cs" /> |
<Compile Include="Cryptography\SHA1.cs" /> |
<Compile Include="Diagnostics\ExceptionExtensions.cs" /> |
<Compile Include="IO\FileSystemWatcher.cs" /> |
<Compile Include="IO\SafeFileStream.cs" /> |
<Compile Include="IO\Utilities\IOExtensions.cs" /> |
<Compile Include="Network\HTTP\HTTPServer.cs" /> |