Initial commit

This commit is contained in:
2014-04-30 16:57:42 -04:00
commit 5c4a1c8b4c
126 changed files with 52769 additions and 0 deletions

62
IO/FileCompare.cs Normal file
View File

@@ -0,0 +1,62 @@
using System;
using System.IO;
namespace Common.IO
{
public class FileCompare
{
#region Comparison
public bool Compare(string fileName1, string fileName2)
{
FileStream fileStream1 = null;
FileStream fileStream2 = null;
bool compareStatus = true;
try
{
// Create file streams for each file
fileStream1 = new FileStream(fileName1, FileMode.Open);
fileStream2 = new FileStream(fileName2, FileMode.Open);
// If the files aren't the same length then don't bother with more
if (fileStream1.Length != fileStream2.Length)
throw new ApplicationException();
// Start comparing the bytes of each file
int fileByte1;
do
{
// Read a byte from the first file
fileByte1 = fileStream1.ReadByte();
// Read a byte from the second file
int fileByte2 = fileStream2.ReadByte();
// If the bytes don't match then stop
if (fileByte1 != fileByte2)
throw new ApplicationException();
}
while (fileByte1 != -1);
}
catch
{
// Compare failed
compareStatus = false;
}
finally
{
// Close both of the file streams
if (fileStream1 != null)
fileStream1.Close();
if (fileStream2 != null)
fileStream2.Close();
}
return compareStatus;
}
#endregion
}
}

107
IO/InterprocessMessage.cs Normal file
View File

@@ -0,0 +1,107 @@
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
namespace Common.IO
{
#region Sender
/// <summary>
/// Helper class for sending a simple interprocess string message
/// </summary>
public static class InterprocessMessageSender
{
public static void SendMessage(string message)
{
string applicationName = Assembly.GetEntryAssembly().GetName().Name;
SendMessage(applicationName, message);
}
public static void SendMessage(string applicationName, string message)
{
// Create the client channel
IpcClientChannel clientChannel = new IpcClientChannel();
// Register the client chanel
ChannelServices.RegisterChannel(clientChannel, false);
// Build a URL string for the remote listener
string messageUrl = string.Format("ipc://{0}/MessageListener", applicationName);
// Get an instance of the remote listener
InterprocessMessageListener messageListener = (InterprocessMessageListener) Activator.GetObject(typeof(InterprocessMessageListener), messageUrl);
// Inform the remote object of the message
messageListener.HandleMessage(message);
// Unregister the channel
ChannelServices.UnregisterChannel(clientChannel);
}
}
#endregion
#region Listener
/// <summary>
/// Helper class for receiving a simple interprocess string message
/// </summary>
public class InterprocessMessageListener : MarshalByRefObject
{
#region Event argument
public class InterprocessMessageEventArgs : EventArgs
{
public string Message { get; private set; }
public InterprocessMessageEventArgs(string message)
{
Message = message;
}
}
#endregion
// The synchronization context of the thread that requested the event
private readonly SynchronizationContext _syncContext;
// Event to be raised when a message has been received
public event EventHandler<InterprocessMessageEventArgs> MessageReceived = delegate { };
public override object InitializeLifetimeService()
{
// Keep the listener around forever
return null;
}
public InterprocessMessageListener(string applicationName)
{
// Store the synchronization context of the current thread
_syncContext = SynchronizationContext.Current;
// Create and register an IPC channel
IpcServerChannel serverChannel = new IpcServerChannel(applicationName);
ChannelServices.RegisterChannel(serverChannel, false);
// Expose the message listener for remoting
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InterprocessMessageListener), "MessageListener", WellKnownObjectMode.Singleton);
RemotingServices.Marshal(this, "MessageListener");
}
public void HandleMessage(string message)
{
// Fire the event on the original thread
_syncContext.Send(delegate
{
// Raise an event with the contents of the message
MessageReceived(this, new InterprocessMessageEventArgs(message));
}, message);
}
}
#endregion
}

18
IO/Keyboard.cs Normal file
View File

@@ -0,0 +1,18 @@
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Common.IO
{
public static class Keyboard
{
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
public static bool IsKeyPressed(Keys keys)
{
short state = GetAsyncKeyState(keys);
return ((state & 0x8000) == 0x8000);
}
}
}