Skip to content

Instantly share code, notes, and snippets.

@jamiepenney
Created January 10, 2019 18:40
Show Gist options
  • Save jamiepenney/5bbbb050404aafa8569fbf42f5f1228d to your computer and use it in GitHub Desktop.
Save jamiepenney/5bbbb050404aafa8569fbf42f5f1228d to your computer and use it in GitHub Desktop.
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfusionDemo
{
class Program
{
static void Main(string[] args)
{
dynamic gateway = GatewayHelper.GetGateway();
gateway.login("My Test Application");
Console.WriteLine("Logged in");
int nextInv = gateway.nextinv(1);
Console.WriteLine("Next invoice number {0}", nextInv);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
public static class GatewayHelper
{
public static dynamic GetGateway()
{
// Attempt to get the latest gateway version registered on the server
string latestGateway = GetLatestRegisteredGateway();
if (string.IsNullOrWhiteSpace(latestGateway))
{
throw new Exception("A registered Infusion Gateway DLL could not be found");
}
// Create the gateway object
Type gatewayType = Type.GetTypeFromProgID(latestGateway);
dynamic ibsGw = Activator.CreateInstance(gatewayType);
// Set the Infusion data path
ibsGw.setpath(@"C:\Infusion\data");
return ibsGw;
}
/// <summary>
/// Gets the registered gateways from the registry.
/// </summary>
/// <returns>gateways in the format of [gatewayname][version].Gateway ie ibsgateway712.Gateway</returns>
private static List<string> GetRegisteredGateways()
{
var keys = Registry.ClassesRoot.GetSubKeyNames().Where(x => x.StartsWith("ibsgateway",
StringComparison.InvariantCultureIgnoreCase));
return keys.ToList();
}
public static string GetLatestRegisteredGateway()
{
string result = string.Empty;
// Get all the registered gateways from the registry
List<string> registeredGateways = GetRegisteredGateways();
// Create the dictionary for holding the registered gateway versions
Dictionary<int, string> versions = new Dictionary<int, string>();
// Loop each of the registered entries
foreach (string registeredGateway in registeredGateways)
{
// Break up the gateway name into an array
// Ie. gwArr[0] = infusiongatewayxxx
// gwArr[1] = Gateway
string[] gwArr = registeredGateway.Split('.'); // Ensure the string was valid and has split ok
if (gwArr.Count() > 0)
{
// Convert to upper case and split on "IBSGATEWAY" portion
// This will leave the version number
string[] versionNumArr = gwArr[0].ToUpper().Split(new string[] { "IBSGATEWAY" }
, StringSplitOptions.RemoveEmptyEntries);
// Ensure the version number arr is populated
if (versionNumArr.Count() > 0)
{
int versionNum = 0;
// Try to parse the string as an integer
if (int.TryParse(versionNumArr[0], out versionNum))
{
// Add the to list of available version if it does not already exist and the original gateway class name
if (!versions.ContainsKey(versionNum))
{
versions.Add(versionNum, registeredGateway);
}
}
}
}
}
// Check the versions list is populated
if (versions.Count > 0)
{
// Order by the highest version and set the result to that value
result = versions.OrderByDescending(x => x.Key).FirstOrDefault().Value;
}
// Return the latest gateway version class name
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment