andrew4582

CommonHelperCode

Sep 9th, 2010
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 47.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Globalization;
  7. using System.Reflection;
  8. using System.IO;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11.  
  12. namespace Core
  13. {
  14.     public class CommonHelper
  15.     {
  16.         public delegate void DefaultDelegate();
  17.         public delegate object DefaultReturnDelegate();
  18.         public delegate void GenericDelegate<TParameterOne>(TParameterOne parameter1);
  19.         public delegate void GenericDelegate<TParameterOne, TParameterTwo>(TParameterOne parameter1, TParameterTwo parameter2);
  20.         public delegate void GenericDelegate<TParameterOne, TParameterTwo, TParameterThree>(TParameterOne parameter1, TParameterTwo parameter2, TParameterThree parameter3);
  21.  
  22.         public delegate TReturnType GenericReturnDelegate<TReturnType>();
  23.         public delegate TReturnType GenericReturnDelegate<TReturnType, TParameterOne>(TParameterOne parameter1);
  24.         public delegate TReturnType GenericReturnDelegate<TReturnType, TParameterOne, TParameterTwo>(TParameterOne parameter1, TParameterTwo parameter2);
  25.  
  26.  
  27.         static System.Text.ASCIIEncoding _encoding = new ASCIIEncoding();
  28.  
  29.         /// <summary>
  30.         /// Lock when accessing. Watch out for potential dead locks, use in atomic operations only.
  31.         /// </summary>
  32.         static Random _random = new Random();
  33.  
  34.         static string _startupExecutingDirectory = string.Empty;
  35.  
  36.         /// <summary>
  37.         /// Only works automatically where entry assembly is available (NA in ApplicationDomain).
  38.         /// </summary>
  39.         public static string StartupExecutingDirectory
  40.         {
  41.             get
  42.             {
  43.                 if (string.IsNullOrEmpty(_startupExecutingDirectory))
  44.                 {
  45.                     Assembly entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
  46.  
  47.                     if (entryAssembly == null)
  48.                     {
  49.                         _startupExecutingDirectory = string.Empty;
  50.                     }
  51.                     else
  52.                     {
  53.                         string assemblyLaunch = entryAssembly.Location;
  54.                         _startupExecutingDirectory = Path.GetDirectoryName(assemblyLaunch);
  55.                     }
  56.                 }
  57.  
  58.                 return _startupExecutingDirectory;
  59.             }
  60.  
  61.             set
  62.             {
  63.                 _startupExecutingDirectory = value;
  64.             }
  65.         }
  66.  
  67.         /// <summary>
  68.         /// How many CPUs, or Cores the CPU has in total.
  69.         /// </summary>
  70.         public static int ProcessorCount
  71.         {
  72.             get
  73.             {
  74.                 return Environment.ProcessorCount;
  75.             }
  76.         }
  77.  
  78.         /// <summary>
  79.         /// An array of time zones
  80.         /// (e.g. new string[] {"PST", "-0700", "(US) Pacific
  81.         /// Standard"}).
  82.         /// </summary>
  83.         protected static string[][] TimeZones = new string[][] {
  84.             new string[] {"ACDT", "+1030", "Australian Central Daylight"},
  85.             new string[] {"ACST", "+0930", "Australian Central Standard"},
  86.             new string[] {"ADT", "-0300", "(US) Atlantic Daylight"},
  87.             new string[] {"AEDT", "+1100", "Australian East Daylight"},
  88.             new string[] {"AEST", "+1000", "Australian East Standard"},
  89.             new string[] {"AHDT", "-0900", ""},
  90.             new string[] {"AHST", "-1000", ""},
  91.             new string[] {"AST", "-0400", "(US) Atlantic Standard"},
  92.             new string[] {"AT", "-0200", "Azores"},
  93.             new string[] {"AWDT", "+0900", "Australian West Daylight"},
  94.             new string[] {"AWST", "+0800", "Australian West Standard"},
  95.             new string[] {"BAT", "+0300", "Bhagdad"},
  96.             new string[] {"BDST", "+0200", "British Double Summer"},
  97.             new string[] {"BET", "-1100", "Bering Standard"},
  98.             new string[] {"BST", "-0300", "Brazil Standard"},
  99.             new string[] {"BT", "+0300", "Baghdad"},
  100.             new string[] {"BZT2", "-0300", "Brazil Zone 2"},
  101.             new string[] {"CADT", "+1030", "Central Australian Daylight"},
  102.             new string[] {"CAST", "+0930", "Central Australian Standard"},
  103.             new string[] {"CAT", "-1000", "Central Alaska"},
  104.             new string[] {"CCT", "+0800", "China Coast"},
  105.             new string[] {"CDT", "-0500", "(US) Central Daylight"},
  106.             new string[] {"CED", "+0200", "Central European Daylight"},
  107.             new string[] {"CET", "+0100", "Central European"},
  108.             new string[] {"CST", "-0600", "(US) Central Standard"},
  109.             new string[] {"CENTRAL", "-0600", "(US) Central Standard"},
  110.             new string[] {"EAST", "+1000", "Eastern Australian Standard"},
  111.             new string[] {"EDT", "-0400", "(US) Eastern Daylight"},
  112.             new string[] {"EED", "+0300", "Eastern European Daylight"},
  113.             new string[] {"EET", "+0200", "Eastern Europe"},
  114.             new string[] {"EEST", "+0300", "Eastern Europe Summer"},
  115.             new string[] {"EST", "-0500", "(US) Eastern Standard"},
  116.             new string[] {"EASTERN", "-0500", "(US) Eastern Standard"},
  117.             new string[] {"FST", "+0200", "French Summer"},
  118.             new string[] {"FWT", "+0100", "French Winter"},
  119.             new string[] {"GMT", "-0000", "Greenwich Mean"},
  120.             new string[] {"ETC", "-0000", "ETC"},
  121.             new string[] {"GST", "+1000", "Guam Standard"},
  122.             new string[] {"HDT", "-0900", "Hawaii Daylight"},
  123.             new string[] {"HST", "-1000", "Hawaii Standard"},
  124.             new string[] {"IDLE", "+1200", "Internation Date Line East"},
  125.             new string[] {"IDLW", "-1200", "Internation Date Line West"},
  126.             new string[] {"IST", "+0530", "Indian Standard"},
  127.             new string[] {"IT", "+0330", "Iran"},
  128.             new string[] {"JST", "+0900", "Japan Standard"},
  129.             new string[] {"JT", "+0700", "Java"},
  130.             new string[] {"MDT", "-0600", "(US) Mountain Daylight"},
  131.             new string[] {"MED", "+0200", "Middle European Daylight"},
  132.             new string[] {"MET", "+0100", "Middle European"},
  133.             new string[] {"MEST", "+0200", "Middle European Summer"},
  134.             new string[] {"MEWT", "+0100", "Middle European Winter"},
  135.             new string[] {"MST", "-0700", "(US) Mountain Standard"},
  136.             new string[] {"MOUNTAIN", "-0700", "(US) Mountain Standard"},
  137.             new string[] {"MT", "+0800", "Moluccas"},
  138.             new string[] {"NDT", "-0230", "Newfoundland Daylight"},
  139.             new string[] {"NFT", "-0330", "Newfoundland"},
  140.             new string[] {"NT", "-1100", "Nome"},
  141.             new string[] {"NST", "+0630", "North Sumatra"},
  142.             new string[] {"NZ", "+1100", "New Zealand "},
  143.             new string[] {"NZST", "+1200", "New Zealand Standard"},
  144.             new string[] {"NZDT", "+1300", "New Zealand Daylight "},
  145.             new string[] {"NZT", "+1200", "New Zealand"},
  146.             new string[] {"PDT", "-0700", "(US) Pacific Daylight"},
  147.             new string[] {"PST", "-0800", "(US) Pacific Standard"},
  148.             new string[] {"PACIFIC", "-0800", "(US) Pacific Standard"},
  149.             new string[] {"ROK", "+0900", "Republic of Korea"},
  150.             new string[] {"SAD", "+1000", "South Australia Daylight"},
  151.             new string[] {"SAST", "+0900", "South Australia Standard"},
  152.             new string[] {"SAT", "+0900", "South Australia Standard"},
  153.             new string[] {"SDT", "+1000", "South Australia Daylight"},
  154.             new string[] {"SST", "+0200", "Swedish Summer"},
  155.             new string[] {"SWT", "+0100", "Swedish Winter"},
  156.             new string[] {"USZ3", "+0400", "USSR Zone 3"},
  157.             new string[] {"USZ4", "+0500", "USSR Zone 4"},
  158.             new string[] {"USZ5", "+0600", "USSR Zone 5"},
  159.             new string[] {"USZ6", "+0700", "USSR Zone 6"},
  160.             new string[] {"UT", "-0000", "Universal Coordinated"},
  161.             new string[] {"UTC", "-0000", "Universal Coordinated"},
  162.             new string[] {"UZ10", "+1100", "USSR Zone 10"},
  163.             new string[] {"WAT", "-0100", "West Africa"},
  164.             new string[] {"WET", "-0000", "West European"},
  165.             new string[] {"WST", "+0800", "West Australian Standard"},
  166.             new string[] {"YDT", "-0800", "Yukon Daylight"},
  167.             new string[] {"YST", "-0900", "Yukon Standard"},
  168.             new string[] {"ZP4", "+0400", "USSR Zone 3"},
  169.             new string[] {"ZP5", "+0500", "USSR Zone 4"},
  170.             new string[] {"ZP6", "+0600", "USSR Zone 5"}
  171.         };
  172.  
  173.         /// <summary>
  174.         /// Is the current OS Windows.
  175.         /// </summary>
  176.         /// <returns></returns>
  177.         public static bool IsRunningOnWindows()
  178.         {
  179.             OperatingSystem osInfo = Environment.OSVersion;
  180.             return osInfo.Platform == PlatformID.Win32NT || osInfo.Platform == PlatformID.Win32S
  181.                 || osInfo.Platform == PlatformID.Win32Windows;
  182.         }
  183.  
  184.         /// <summary>
  185.         /// Check to see if you are running on a 32b or a 64b OS.
  186.         /// </summary>
  187.         /// <returns></returns>
  188.         public static bool IsRunningOn64bOS()
  189.         {
  190.             return IntPtr.Size == 8;
  191.         }
  192.  
  193.         /// <summary>
  194.         /// This is related to comparing IComparable inheritors, that may be also null.
  195.         /// </summary>
  196.         /// <param name="item1"></param>
  197.         /// <param name="item2"></param>
  198.         /// <returns></returns>
  199.         public static int CompareNullable(IComparable item1, IComparable item2)
  200.         {
  201.             if (item1 != null && item2 == null)
  202.             {
  203.                 return 1;
  204.             }
  205.             else if (item1 == null && item2 != null)
  206.             {
  207.                 return -1;
  208.             }
  209.             else if (item1 == null && item2 == null)
  210.             {
  211.                 return 0;
  212.             }
  213.  
  214.             return item1.CompareTo(item2);
  215.         }
  216.  
  217.         /// <summary>
  218.         /// Replaces {DateTime} in input strin with a file name compatible date time.
  219.         /// </summary>
  220.         /// <param name="inputString"></param>
  221.         /// <param name="dateTime"></param>
  222.         /// <returns></returns>
  223.         public static string ReplaceFileNameCompatibleDateTime(string inputString, DateTime dateTime)
  224.         {
  225.             if (inputString.Contains("{DateTime}") || inputString.Contains("{Datetime}"))
  226.             {
  227.                 string dateTimeString = GetFileCompatibleDateTime(dateTime);
  228.  
  229.                 inputString = inputString.Replace("{DateTime}", dateTimeString);
  230.                 inputString = inputString.Replace("{Datetime}", dateTimeString);
  231.             }
  232.  
  233.             return inputString;
  234.         }
  235.  
  236.         /// <summary>
  237.         /// Get date time in a file name compatible fashion, so a file can be created from it.
  238.         /// </summary>
  239.         public static string GetFileCompatibleDateTime(DateTime dateTime)
  240.         {
  241.             return dateTime.Year.ToString() + '-' + dateTime.Month.ToString("00")
  242.                 + '-' + dateTime.Day.ToString("00") + "." + dateTime.Hour.ToString("00")
  243.                 + '-' + dateTime.Minute.ToString("00") + '-' + dateTime.Second.ToString("00") + '.' + dateTime.Millisecond.ToString();
  244.         }
  245.  
  246.  
  247.         #region Time and Number Helpers
  248.  
  249.         static public NumberFormatInfo UniversalNumberFormatInfo = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
  250.  
  251.         static public CultureInfo InvariantFormatProvider = CultureInfo.InvariantCulture;
  252.  
  253.         static readonly DateTime Time1970 = new DateTime(1970, 1, 1);
  254.         public static DateTime? GenerateDateTimeSecondsFrom1970(Int64? secondsFrom1970)
  255.         {
  256.             if (secondsFrom1970.HasValue == false)
  257.             {
  258.                 return null;
  259.             }
  260.             else
  261.             {
  262.                 return Time1970.AddSeconds(secondsFrom1970.Value);
  263.             }
  264.         }
  265.  
  266.         public static Int64 GenerateSecondsDateTimeFrom1970(DateTime time)
  267.         {
  268.             TimeSpan span = time - Time1970;
  269.             return (long)span.TotalSeconds;
  270.         }
  271.  
  272.  
  273.  
  274.         /// <summary>
  275.         /// Convert a secure string into normal string.
  276.         /// </summary>
  277.         /// <param name="secureString"></param>
  278.         /// <returns></returns>
  279.         static public string SecureStringToString(SecureString secureString)
  280.         {
  281.             IntPtr pointer = Marshal.SecureStringToBSTR(secureString);
  282.             return Marshal.PtrToStringUni(pointer);
  283.         }
  284.  
  285.         /// <summary>
  286.         /// Convert a secure string into normal string.
  287.         /// </summary>
  288.         /// <param name="secureString"></param>
  289.         /// <returns></returns>
  290.         static public SecureString StringToSecureString(string value)
  291.         {
  292.             SecureString result = new SecureString();
  293.             foreach (Char c in value)
  294.             {
  295.                 result.AppendChar(c);
  296.             }
  297.             return result;
  298.         }
  299.  
  300.         /// <summary>
  301.         /// Convert a secure string into a byte array.
  302.         /// </summary>
  303.         /// <param name="secureString"></param>
  304.         /// <returns></returns>
  305.         static public byte[] SecureStringToByte(SecureString secureString)
  306.         {
  307.             lock (_encoding)
  308.             {
  309.                 return _encoding.GetBytes(SecureStringToString(secureString));
  310.             }
  311.         }
  312.  
  313.         /// <summary>
  314.         /// Is this a nullable "TypeName?" type of type.
  315.         /// </summary>
  316.         /// <param name="type"></param>
  317.         /// <returns></returns>
  318.         public static bool IsNullableType(Type type)
  319.         {
  320.             return Nullable.GetUnderlyingType(type) != null;
  321.         }
  322.  
  323.         /// <summary>
  324.         /// Generate an array containing the same value in all members.
  325.         /// </summary>
  326.         public static ItemType[] GenerateSingleValueArray<ItemType>(int lenght, ItemType value)
  327.         {
  328.             ItemType[] result = new ItemType[lenght];
  329.             for (int i = 0; i < lenght; i++)
  330.             {
  331.                 result[i] = value;
  332.             }
  333.  
  334.             return result;
  335.         }
  336.  
  337.         #endregion
  338.  
  339.         /// <summary>
  340.         /// Allows to uniquely identify a method.
  341.         /// </summary>
  342.         /// <param name="methodInfo"></param>
  343.         /// <param name="omitEventMethodPrefix">Should the provided name omit the "add_" and "_remove" prefix of the method (if it has it).</param>
  344.         /// <returns></returns>
  345.         public static string GetMethodExtendedName(MethodInfo methodInfo, bool includeAssemblyInfo)
  346.         {
  347.             Type reflectedType = methodInfo.ReflectedType;
  348.             string methodName = methodInfo.ToString();
  349.  
  350.             if (includeAssemblyInfo)
  351.             {
  352.                 AssemblyName assemblyName = reflectedType.Module.Assembly.GetName();
  353.                 return assemblyName.Name + "[" + assemblyName.Version + "]" + reflectedType.Namespace + "." +
  354.                     reflectedType.Name + "." + methodInfo.ToString();
  355.             }
  356.  
  357.             return reflectedType.Namespace + "." + reflectedType.Name + "." + methodInfo.ToString();
  358.         }
  359.  
  360.         /// <summary>
  361.         ///
  362.         /// </summary>
  363.         public static string GetAssemblyGuidString(Assembly assembly)
  364.         {
  365.             object[] objects = assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
  366.             if (objects.Length == 0)
  367.             {
  368.                 return String.Empty;
  369.             }
  370.             return ((System.Runtime.InteropServices.GuidAttribute)objects[0]).Value;
  371.         }
  372.  
  373.         /// <summary>
  374.         /// Obtain the name of an event method.
  375.         /// </summary>
  376.         public static string GetEventExtendedNameByMethod(MethodInfo methodInfo, bool includeAssemblyInfo, bool omitEventMethodPrefix)
  377.         {
  378.             Type reflectedType = methodInfo.ReflectedType;
  379.             string methodName = methodInfo.ToString();
  380.  
  381.             if (methodName.Contains(" add_"))
  382.             {
  383.                 if (omitEventMethodPrefix)
  384.                 {
  385.                     methodName = methodName.Replace(" add_", " ");
  386.                 }
  387.             }
  388.             else if (methodName.Contains(" remove_"))
  389.             {
  390.                 if (omitEventMethodPrefix)
  391.                 {
  392.                     methodName = methodName.Replace(" remove_", " ");
  393.                 }
  394.             }
  395.             else
  396.             {// Not an event method.
  397.                 return string.Empty;
  398.             }
  399.  
  400.             if (includeAssemblyInfo)
  401.             {
  402.                 AssemblyName assemblyName = reflectedType.Module.Assembly.GetName();
  403.                 return assemblyName.Name + "[" + assemblyName.Version + "]" + reflectedType.Namespace + "." +
  404.                     reflectedType.Name + "." + methodName;
  405.             }
  406.  
  407.             return reflectedType.Namespace + "." + reflectedType.Name + "." + methodName;
  408.         }
  409.  
  410.         /// <summary>
  411.         ///
  412.         /// </summary>
  413.         public static string GetExceptionMessage(Exception exception)
  414.         {
  415.             if (exception == null)
  416.             {
  417.                 return StringHelper.NoVallueString;
  418.             }
  419.  
  420.             string message = "Exception [" + exception.GetType().Name;
  421.             if (string.IsNullOrEmpty(exception.Message) == false)
  422.             {
  423.                 message += ", " + exception.Message;
  424.             }
  425.             message += "]";
  426.  
  427.             if (exception.InnerException != null)
  428.             {
  429.                 message += ", Inner [" + exception.InnerException.GetType().Name;
  430.                 if (string.IsNullOrEmpty(exception.InnerException.Message) == false)
  431.                 {
  432.                     message += ", " + exception.InnerException.Message;
  433.                 }
  434.             }
  435.             message += "]";
  436.  
  437.             return message;
  438.         }
  439.  
  440.         /// <summary>
  441.         ///
  442.         /// </summary>
  443.         public static string GetEventMethodExtendedName(EventInfo eventInfo, bool includeAssemblyInfo)
  444.         {
  445.             //Type reflectedType = eventInfo.ReflectedType;
  446.             //string methodName = eventInfo.Name;
  447.  
  448.             // We shall use the add event method and reuse the GetEventExtendedNameByMethod() to generate the full name.
  449.             MethodInfo addMethodInfo = eventInfo.GetAddMethod();
  450.             return GetEventExtendedNameByMethod(addMethodInfo, includeAssemblyInfo, true);
  451.         }
  452.  
  453.         /// <summary>
  454.         /// Helper, converts exception details to string message.
  455.         /// </summary>
  456.         public static string ProcessExceptionMessage(string errorDetails, Exception exception)
  457.         {
  458.             if (string.IsNullOrEmpty(errorDetails) == false)
  459.             {
  460.                 return errorDetails + ", " + CommonHelper.GetExceptionMessage(exception);
  461.             }
  462.  
  463.             return GetExceptionMessage(exception);
  464.         }
  465.  
  466.         /// <summary>
  467.         /// Convert a set of parameters to string.
  468.         /// </summary>
  469.         public static string ParamsToString(params string[] parameters)
  470.         {
  471.             string result = "";
  472.             if (parameters != null && parameters.Length > 0)
  473.             {
  474.                 for (int i = 0; i < parameters.Length; i++)
  475.                 {
  476.                     if (i == 0)
  477.                     {
  478.                         result += parameters[i];
  479.                     }
  480.                     else
  481.                     {
  482.                         result += ", " + parameters[i];
  483.                     }
  484.                 }
  485.             }
  486.             return result;
  487.         }
  488.  
  489.         /// <summary>
  490.         /// Inclusive min, *exclusive* max.
  491.         /// </summary>
  492.         /// <param name="min"></param>
  493.         /// <param name="max"></param>
  494.         /// <returns></returns>
  495.         public static int Random(int min, int max)
  496.         {
  497.             lock (_random)
  498.             {
  499.                 return _random.Next(min, max);
  500.             }
  501.         }
  502.  
  503.         /// <summary>
  504.         /// Helper, generates a random decimal.
  505.         /// </summary>
  506.         /// <param name="min"></param>
  507.         /// <param name="max"></param>
  508.         /// <returns></returns>
  509.         public static decimal Random(decimal min, decimal max)
  510.         {
  511.             decimal randomValue;
  512.             lock (_random)
  513.             {
  514.                 randomValue = (decimal)_random.NextDouble();
  515.             }
  516.  
  517.             decimal difference = max - min;
  518.             return min + difference * randomValue;
  519.         }
  520.  
  521.         /// <summary>
  522.         /// Helper, generates a random double.
  523.         /// </summary>
  524.         /// <param name="min"></param>
  525.         /// <param name="max"></param>
  526.         /// <returns></returns>
  527.         public static double Random(double min, double max)
  528.         {
  529.             lock (_random)
  530.             {
  531.                 double randomValue = _random.NextDouble();
  532.                 double difference = max - min;
  533.  
  534.                 return min + difference * randomValue;
  535.             }
  536.         }
  537.  
  538.         /// <summary>
  539.         /// Helper, swaps 2 objects values.
  540.         /// </summary>
  541.         /// <typeparam name="TType"></typeparam>
  542.         /// <param name="value1"></param>
  543.         /// <param name="value2"></param>
  544.         public static void Swap<TType>(ref TType value1, ref TType value2)
  545.         {
  546.             TType cache = value1;
  547.             value1 = value2;
  548.             value2 = cache;
  549.         }
  550.  
  551.         /// <summary>
  552.         /// Helper, generates a random number; max is Exclusive.
  553.         /// </summary>
  554.         public static int Random(int max)
  555.         {
  556.             lock (_random)
  557.             {
  558.                 return _random.Next(max);
  559.             }
  560.         }
  561.  
  562.         /// <summary>
  563.         /// Run the given url.
  564.         /// </summary>
  565.         /// <param name="url"></param>
  566.         public static void RunUrl(string url)
  567.         {
  568.             // Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode, HttpUtility.UrlPathEncode
  569.             //string sUrl = HttpUtility.UrlDecode(url);
  570.             System.Diagnostics.Process.Start(url);
  571.         }
  572.  
  573.         /// <summary>
  574.         /// Map relative path using as basis provided assembly location.
  575.         /// </summary>
  576.         public static string MapRelativeFilePathToAssemblyDirectory(Assembly assembly, string path)
  577.         {
  578.             if (Path.IsPathRooted(path))
  579.             {
  580.                 return path;
  581.             }
  582.  
  583.             return Path.Combine(Path.GetDirectoryName(assembly.Location), path);
  584.         }
  585.  
  586.         /// <summary>
  587.         /// Map relative path using as basis entry assembly location.
  588.         /// </summary>
  589.         /// <param name="path"></param>
  590.         /// <returns></returns>
  591.         public static string MapRelativeFilePathToStartupDirectory(string path)
  592.         {
  593.             if (Path.IsPathRooted(path))
  594.             {
  595.                 return path;
  596.             }
  597.  
  598.             string assemblyLaunch = StartupExecutingDirectory;
  599.             // GetGullPath needed to convert any ../ elements.
  600.             return Path.GetFullPath(Path.Combine(assemblyLaunch, path));
  601.         }
  602.  
  603.         /// <summary>
  604.         /// See SeparateCapitalLetters().
  605.         /// </summary>
  606.         /// <param name="input"></param>
  607.         /// <returns></returns>
  608.         public static string SplitCapitalLetters(string input)
  609.         {
  610.             return SeparateCapitalLetters(input);
  611.         }
  612.  
  613.         /// <summary>
  614.         /// Helper, will return a string that has the conjoined capital letters in the input
  615.         /// string separated into separate words.
  616.         /// </summary>
  617.         public static string SeparateCapitalLetters(string input)
  618.         {
  619.             StringBuilder result = new StringBuilder();
  620.  
  621.             char previousChar = ' ';
  622.             foreach (char c in input)
  623.             {
  624.                 if (c.ToString() == c.ToString().ToUpper() && previousChar != ' ')
  625.                 {
  626.                     result.Append(" ");
  627.                 }
  628.  
  629.                 result.Append(c);
  630.                 previousChar = c;
  631.             }
  632.  
  633.             return result.ToString();
  634.         }
  635.  
  636.         /// <summary>
  637.         /// Helper method, looks for a full match of a string inside a collection of other strings.
  638.         /// </summary>
  639.         public static bool ContainsString(string stringValue, IEnumerable<string> values)
  640.         {
  641.             foreach (string n in values)
  642.             {
  643.                 if (n == stringValue)
  644.                 {
  645.                     return true;
  646.                 }
  647.             }
  648.             return false;
  649.         }
  650.  
  651.         public static string GenerateUniqueName(string initialName, string[] existingNames)
  652.         {
  653.             if (ContainsString(initialName, existingNames))
  654.             {// We need to generate a new name.
  655.                 string tempName = initialName;
  656.                 for (int i = 0; i < 9999; i++)
  657.                 {
  658.                     if (ContainsString(tempName + i.ToString(), existingNames) == false)
  659.                     {// Ok, found unique one.
  660.                         tempName += i.ToString();
  661.                         break;
  662.                     }
  663.                 }
  664.                 initialName = tempName;
  665.             }
  666.             return initialName;
  667.         }
  668.  
  669.         /// <summary>
  670.         /// Helper, converts enumerable items collection into a list of the same.
  671.         /// </summary>
  672.         static public List<TDataType> EnumerableToList<TDataType>(System.Collections.IEnumerable enumerable)
  673.         {
  674.             List<TDataType> list = new List<TDataType>();
  675.             foreach (TDataType value in enumerable)
  676.             {
  677.                 list.Add(value);
  678.             }
  679.  
  680.             return list;
  681.         }
  682.  
  683.         /// <summary>
  684.         /// Helper, converts enumerable items collection into a list of the same.
  685.         /// </summary>
  686.         static public List<TDataType> EnumerableToList<TDataType>(IEnumerable<TDataType> enumerable)
  687.         {
  688.             List<TDataType> list = new List<TDataType>();
  689.             foreach (TDataType value in enumerable)
  690.             {
  691.                 list.Add(value);
  692.             }
  693.             return list;
  694.         }
  695.  
  696.  
  697.         /// <summary>
  698.         /// Helper, converts enumerable items collection into an array of the same.
  699.         /// </summary>
  700.         //[Obsolete("Use EnumerableToList for improved speed and functionality.")]
  701.         static public TDataType[] EnumerableToArray<TDataType>(IEnumerable<TDataType> enumerable)
  702.         {
  703.             List<TDataType> list = new List<TDataType>();
  704.             foreach (TDataType value in enumerable)
  705.             {
  706.                 list.Add(value);
  707.             }
  708.             return list.ToArray();
  709.         }
  710.  
  711.         /// <summary>
  712.         /// Helper, searches an enumerable collection for a given item; usefull for arrays.
  713.         /// </summary>
  714.         /// <typeparam name="TDataType"></typeparam>
  715.         /// <param name="enumerable"></param>
  716.         /// <returns></returns>
  717.         public static bool EnumerableContains<TDataType>(IEnumerable<TDataType> enumerable, TDataType item)
  718.             where TDataType : IEquatable<TDataType>
  719.         {
  720.             foreach (TDataType enumerableItem in enumerable)
  721.             {
  722.                 if (enumerableItem.Equals(item))
  723.                 {
  724.                     return true;
  725.                 }
  726.             }
  727.  
  728.             return false;
  729.         }
  730.  
  731.         /// <summary>
  732.         /// Provides the first element in a enumerable of items.
  733.         /// </summary>
  734.         static public TDataType EnumerableFirst<TDataType>(IEnumerable<TDataType> enumerable)
  735.             where TDataType : class
  736.         {
  737.             foreach (TDataType item in enumerable)
  738.             {
  739.                 return item;
  740.             }
  741.  
  742.             return null;
  743.         }
  744.  
  745.         /// <summary>
  746.         /// Get the first element of an enumerable, or throw exception.
  747.         /// </summary>
  748.         static public TDataType EnumerableFirstThrows<TDataType>(IEnumerable<TDataType> enumerable)
  749.         {
  750.             foreach (TDataType item in enumerable)
  751.             {
  752.                 return item;
  753.             }
  754.  
  755.             throw new Exception("Invalid enumeration.");
  756.         }
  757.  
  758.  
  759.  
  760.         /// <summary>
  761.         /// Helper, atomic read operation of long value.
  762.         /// </summary>
  763.         static public long AtomicRead(ref long value)
  764.         {
  765.             return Interlocked.Read(ref value);
  766.         }
  767.  
  768.         /// <summary>
  769.         /// Atomic read operation.
  770.         /// </summary>
  771.         /// <param name="?"></param>
  772.         /// <returns></returns>
  773.         static public double AtomicRead(ref double value)
  774.         {
  775.             double result = 0;
  776.             Interlocked.Exchange(ref result, value);
  777.             return result;
  778.         }
  779.  
  780.         static public float[] DoublesToFloats(double[] values)
  781.         {
  782.             float[] result = new float[values.Length];
  783.             for (int i = 0; i < result.Length; i++)
  784.             {
  785.                 result[i] = (float)values[i];
  786.             }
  787.             return result;
  788.         }
  789.  
  790.         static public double[] FloatsToDoubles(float[] values)
  791.         {
  792.             double[] result = new double[values.Length];
  793.             for (int i = 0; i < result.Length; i++)
  794.             {
  795.                 result[i] = values[i];
  796.             }
  797.             return result;
  798.         }
  799.  
  800.         /// <summary>
  801.         /// Get the values of the combined enum value.
  802.         /// Make sure to have a int based enum.
  803.         /// </summary>
  804.         static public List<TEnumType> GetCombinedEnumValues<TEnumType>(TEnumType value)
  805.         {
  806.             List<TEnumType> result = new List<TEnumType>();
  807.             foreach (object enumValue in Enum.GetValues(value.GetType()))
  808.             {
  809.                 if (((int)enumValue & (int)((object)value)) != 0)
  810.                 {
  811.                     result.Add((TEnumType)enumValue);
  812.                 }
  813.             }
  814.  
  815.             return result;
  816.         }
  817.  
  818.         /// <summary>
  819.         /// Get the name of the combined enum value.
  820.         /// Make sure to have a int based enum.
  821.         /// </summary>
  822.         static public string GetCombinedEnumName(Type enumType, int value)
  823.         {
  824.             string result = string.Empty;
  825.             string[] names = Enum.GetNames(enumType);
  826.             Array values = Enum.GetValues(enumType);
  827.             for (int i = 0; i < names.Length; i++)
  828.             {
  829.                 if ((value & (int)values.GetValue(i)) != 0)
  830.                 {
  831.                     if (string.IsNullOrEmpty(result) == false)
  832.                     {
  833.                         result += ", ";
  834.                     }
  835.                     result += names[i];
  836.                 }
  837.             }
  838.  
  839.             return result;
  840.         }
  841.  
  842.         /// <summary>
  843.         /// Use this helper you can, without specifying the template parameters, just pass them in,
  844.         /// the compiler will figure it out.
  845.         /// </summary>
  846.         public static KeyValuePair<ItemType1, ItemType2> CreatePair<ItemType1, ItemType2>(ItemType1 item1, ItemType2 item2)
  847.             where ItemType1 : class
  848.             where ItemType2 : class
  849.         {
  850.             return new KeyValuePair<ItemType1, ItemType2>(item1, item2);
  851.         }
  852.  
  853.         public static DefaultDelegate CreateDefaultDelegate(DefaultDelegate target)
  854.         {
  855.             return new DefaultDelegate(target);
  856.         }
  857.  
  858.         /// <summary>
  859.         /// Allows to iterate a collection of parent type items and return only the ones that are child typed.
  860.         /// </summary>
  861.         /// <typeparam name="TParentType"></typeparam>
  862.         /// <param name="parentCollection"></param>
  863.         /// <returns></returns>
  864.         static public IEnumerable<TChildType> SafeChildTypeIteration<TParentType, TChildType>(IEnumerable<TParentType> parentCollection)
  865.             where TParentType : class
  866.             where TChildType : TParentType
  867.         {
  868.             foreach (TParentType item in parentCollection)
  869.             {
  870.                 if (item is TChildType)
  871.                 {
  872.                     yield return (TChildType)item;
  873.                 }
  874.             }
  875.         }
  876.  
  877.         static public Stack<T> CloneStack<T>(Stack<T> inputStack)
  878.         {
  879.             Stack<T> result = new Stack<T>();
  880.  
  881.             T[] array = inputStack.ToArray();
  882.             for (int i = array.Length - 1; i >= 0; i--)
  883.             {
  884.                 result.Push(array[i]);
  885.             }
  886.             return result;
  887.         }
  888.  
  889.         static public bool CompareArrays<ArrayType>(ArrayType[] array1, ArrayType[] array2)
  890.             where ArrayType : IEquatable<ArrayType>
  891.         {
  892.             System.Diagnostics.Debug.Assert(array1.Length == array2.Length);
  893.             for (int i = 0; i < array1.Length; i++)
  894.             {
  895.                 if (array1[i].Equals(array2[i]) == false)
  896.                 {
  897.                     return false;
  898.                 }
  899.             }
  900.  
  901.             return true;
  902.         }
  903.  
  904.         static public double[] Sort(IEnumerable<double> inputArray, bool reverseOrder)
  905.         {
  906.             List<double> doubles = new List<double>(inputArray);
  907.             doubles.Sort();
  908.             if (reverseOrder)
  909.             {
  910.                 doubles.Reverse();
  911.             }
  912.             return doubles.ToArray();
  913.         }
  914.  
  915.         /// <summary>
  916.         /// Limit a value to be inside a range.
  917.         /// </summary>
  918.         static public decimal LimitRange(decimal value, decimal min, decimal max)
  919.         {
  920.             if (value > max)
  921.             {
  922.                 return max;
  923.             }
  924.  
  925.             if (value < min)
  926.             {
  927.                 return min;
  928.             }
  929.  
  930.             return value;
  931.         }
  932.  
  933.         /// <summary>
  934.         /// Helper, returns the Min, but considering if there is a value or not.
  935.         /// </summary>
  936.         static public decimal? Min(decimal? value1, decimal? value2)
  937.         {
  938.             if (value1.HasValue && value2.HasValue)
  939.             {
  940.                 return Math.Min(value1.Value, value2.Value);
  941.             }
  942.             else if (value1.HasValue)
  943.             {
  944.                 return value1.Value;
  945.             }
  946.             else if (value2.HasValue)
  947.             {
  948.                 return value2.Value;
  949.             }
  950.  
  951.             return null;
  952.         }
  953.  
  954.         /// <summary>
  955.         /// Helper, returns the Max, but considering if there is a value or not.
  956.         /// </summary>
  957.         static public decimal? Max(decimal? value1, decimal? value2)
  958.         {
  959.             if (value1.HasValue && value2.HasValue)
  960.             {
  961.                 return Math.Max(value1.Value, value2.Value);
  962.             }
  963.             else if (value1.HasValue)
  964.             {
  965.                 return value1.Value;
  966.             }
  967.             else if (value2.HasValue)
  968.             {
  969.                 return value2.Value;
  970.             }
  971.  
  972.             return null;
  973.         }
  974.  
  975.         static public void GetMinMax(IEnumerable<double> values, out double min, out double max)
  976.         {
  977.             min = double.MaxValue;
  978.             max = double.MinValue;
  979.  
  980.             foreach (double value in values)
  981.             {
  982.                 min = Math.Min(min, value);
  983.                 max = Math.Max(max, value);
  984.             }
  985.         }
  986.  
  987.         static public float[] IntsToFloats(int[] values)
  988.         {
  989.             float[] result = new float[values.Length];
  990.             for (int i = 0; i < result.Length; i++)
  991.             {
  992.                 result[i] = values[i];
  993.             }
  994.             return result;
  995.         }
  996.  
  997.         static public double[] IntsToDoubles(int[] values)
  998.         {
  999.             double[] result = new double[values.Length];
  1000.             for (int i = 0; i < result.Length; i++)
  1001.             {
  1002.                 result[i] = values[i];
  1003.             }
  1004.             return result;
  1005.         }
  1006.  
  1007.         static public float[] DecimalsToFloats(IEnumerable<decimal> values, int count)
  1008.         {
  1009.             float[] result = new float[count];
  1010.             int i = 0;
  1011.             foreach (decimal value in values)
  1012.             {
  1013.                 result[i] = (float)value;
  1014.                 i++;
  1015.             }
  1016.             return result;
  1017.         }
  1018.  
  1019.         static public float[] DoublesToFloats(IEnumerable<double> values, int count)
  1020.         {
  1021.             float[] result = new float[count];
  1022.             int i = 0;
  1023.             foreach (double value in values)
  1024.             {
  1025.                 result[i] = (float)value;
  1026.                 i++;
  1027.             }
  1028.             return result;
  1029.         }
  1030.  
  1031.         /// <summary>
  1032.         /// Helper, sometimes decimal encoding is used as minValue (or maxValue) instead of null structure.
  1033.         /// </summary>
  1034.         /// <param name="value"></param>
  1035.         /// <returns></returns>
  1036.         public static Decimal? ValueDencodeConvert(Decimal value)
  1037.         {
  1038.             if (value != Decimal.MinValue && value != decimal.MaxValue)
  1039.             {
  1040.                 return value;
  1041.             }
  1042.             else
  1043.             {
  1044.                 return null;
  1045.             }
  1046.         }
  1047.  
  1048.         /// <summary>
  1049.         /// See ValueEncodeConvert.
  1050.         /// </summary>
  1051.         public static Decimal ValueEncodeConvert(Decimal? value)
  1052.         {
  1053.             if (value == null)
  1054.             {
  1055.                 return decimal.MinValue;
  1056.             }
  1057.             else
  1058.             {
  1059.                 return value.Value;
  1060.             }
  1061.         }
  1062.  
  1063.         /// <summary>
  1064.         /// Application version string, may fail if entry assembly not available.
  1065.         /// </summary>
  1066.         public static string ApplicationVersion
  1067.         {
  1068.             get
  1069.             {
  1070.                 Assembly entryAssembly = Assembly.GetEntryAssembly();
  1071.                 if (entryAssembly != null)
  1072.                 {
  1073.                     return entryAssembly.GetName().Version.ToString();
  1074.                 }
  1075.  
  1076.                 return string.Empty;
  1077.             }
  1078.         }
  1079.  
  1080.         /// <summary>
  1081.         /// Helper, will remove any symbols not allowed to be used in a file name.
  1082.         /// </summary>
  1083.         /// <param name="input"></param>
  1084.         /// <returns></returns>
  1085.         public static string RepairFileName(string input)
  1086.         {
  1087.             foreach (char c in Path.GetInvalidFileNameChars())
  1088.             {
  1089.                 input = input.Replace(c, ' ');
  1090.             }
  1091.  
  1092.             return input;
  1093.         }
  1094.  
  1095.         public static string RepairHTMLString(string input)
  1096.         {
  1097.             if (string.IsNullOrEmpty(input))
  1098.             {
  1099.                 return string.Empty;
  1100.             }
  1101.  
  1102.             // Replace special HTML symbols.
  1103.             input = input.Replace("&amp;", "&");
  1104.             // Remove tags.
  1105.             input = Regex.Replace(input, "<(.|\n)*?((>(.|\n)*?</(.|\n)*?>)|(/>))", "");
  1106.  
  1107.             return WebHelper.HtmlDecode(input);
  1108.         }
  1109.  
  1110.         /// <summary>
  1111.         /// This allows parsing with taking TimeZones into the string in consideration.
  1112.         /// </summary>
  1113.         public static DateTime ParseDateTimeWithZone(string dateTime)
  1114.         {
  1115.             DateTime result = DateTime.MinValue;
  1116.  
  1117.             try
  1118.             {
  1119.  
  1120.                 dateTime = dateTime.Trim();
  1121.  
  1122.                 if (string.IsNullOrEmpty(dateTime))
  1123.                 {
  1124.                     return result;
  1125.                 }
  1126.  
  1127.                 if (dateTime.Contains("/"))
  1128.                 {// Some date time formats contain 2 zones like "Etc/GMT" - fix that here.
  1129.                     dateTime = dateTime.Substring(0, dateTime.IndexOf("/"));
  1130.                 }
  1131.  
  1132.                 if (dateTime.Contains("."))
  1133.                 {// Some date time formats contain a dot, clear it.
  1134.                     dateTime = dateTime.Replace(".", "");
  1135.                 }
  1136.  
  1137.                 if (dateTime.Contains("Thur"))
  1138.                 {// Thur occurs in some feeds and is not recognized.
  1139.                     dateTime = dateTime.Replace("Thur", "");
  1140.                 }
  1141.  
  1142.                 // The dateTime parameter is either local (no time
  1143.                 // zone specified), or it has a time zone.
  1144.                 // Use the regex to examine the last part of the
  1145.                 // dateTime string and determine which category it falls into.
  1146.  
  1147.                 Match m = Regex.Match(dateTime.Trim(), @"(\b\w{3,4}|[+-]?\d{4})$");
  1148.                 //Match m = Regex.Match(dateTime.Trim(), @"(\b\w{3,4})$");
  1149.  
  1150.                 if (m.Value == DateTime.Now.Year.ToString() ||
  1151.                     m.Value == (DateTime.Now.Year - 1).ToString() ||
  1152.                     m.Value == (DateTime.Now.Year - 2).ToString() ||
  1153.                         m.Value == (DateTime.Now.Year - 3).ToString())
  1154.                 {// Sometimes the year is passed this way and the algo confuses it with a timizing zone.
  1155.                     m = null;
  1156.                 }
  1157.  
  1158.                 if (m == null || m.Length == 0)
  1159.                 {
  1160.                     //result = DateTime.Parse(dateTime);
  1161.                     if (DateTimeHelper.TryParse(dateTime, DateTimeHelper.DateTimeFormat.USA_DATE, out result) == false)
  1162.                     {
  1163.                         result = DateTime.MinValue;
  1164.                     }
  1165.                 }
  1166.                 else
  1167.                 {
  1168.                     // Date w/ time zone. m.Value holds the time zone info
  1169.                     // (either a time zone name (e.g. PST), or the
  1170.                     // numeric offset (e.g. -0800).
  1171.                     result = ConvertToLocalDateTime(dateTime, m.Value);
  1172.                 }
  1173.             }
  1174.             catch (Exception ex)
  1175.             {
  1176.                 CoreSystemMonitor.OperationError("Failed to parse time.", ex);
  1177.             }
  1178.  
  1179.             return result;
  1180.         }
  1181.  
  1182.         /// <summary>
  1183.         ///
  1184.         /// </summary>
  1185.         static public string GetShortDateTimeNoYear(DateTime? dateTime)
  1186.         {
  1187.             if (dateTime.HasValue == false)
  1188.             {
  1189.                 return string.Empty;
  1190.             }
  1191.             //if (dateTime.DayOfYear == DateTime.Now.DayOfYear && dateTime.Year == DateTime.Now.Year)
  1192.             //{
  1193.             //    return dateTime.ToString("HH:mm");
  1194.             //}
  1195.             //else
  1196.             //{
  1197.             return dateTime.Value.ToString("dd MMM HH:mm");
  1198.             //}
  1199.         }
  1200.  
  1201.         /// <summary>
  1202.         ///
  1203.         /// </summary>
  1204.         static public string GetShortDateTime(DateTime? dateTime)
  1205.         {
  1206.             if (dateTime.HasValue)
  1207.             {
  1208.                 return dateTime.Value.Day.ToString("00") + "/" + dateTime.Value.Month + "/" + dateTime.Value.Year + " " + dateTime.Value.Hour.ToString("00") + ":" + dateTime.Value.Minute.ToString("00");
  1209.             }
  1210.             else
  1211.             {
  1212.                 return StringHelper.NoVallueAssignedString;
  1213.             }
  1214.         }
  1215.  
  1216.         /// <summary>
  1217.         /// Helper.
  1218.         /// </summary>
  1219.         /// <param name="dateTime"></param>
  1220.         /// <param name="timeZoneId"></param>
  1221.         /// <returns></returns>
  1222.         private static DateTime ConvertToLocalDateTime(string dateTime, string timeZoneId)
  1223.         {
  1224.             // Strip the time zone ID from the end of the dateTime string.
  1225.             dateTime = dateTime.Replace(timeZoneId, "").Trim();
  1226.  
  1227.             // Convert the timeZoneId to a TimeSpan.
  1228.             // (Leading + signs aren't allowed in the TimeSpan.Parse
  1229.             // parameter, although leading - signs are.
  1230.             // The purpose of the [+]*? at the beginning of the
  1231.             // regex is to account for, and ignore, any leading + sign).
  1232.  
  1233.             string ts = Regex.Replace(GetTimeZoneOffset(timeZoneId),
  1234.             @"^[+]*?(?<hours>[-]?\d\d)(?<minutes>\d\d)$",
  1235.             "${hours}:${minutes}:00");
  1236.             TimeSpan timeZoneOffset = TimeSpan.Parse(ts);
  1237.  
  1238.             TimeSpan localUtcOffset =
  1239.             TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
  1240.  
  1241.             // Get the absolute time difference between the given
  1242.             // datetime's time zone and the local datetime's time zone.
  1243.  
  1244.             TimeSpan absoluteOffset = timeZoneOffset - localUtcOffset;
  1245.             absoluteOffset = absoluteOffset.Duration();
  1246.  
  1247.             // Now that the absolute time difference is known,
  1248.             // determine whether to add or subtract it from the
  1249.             // given dateTime, and then return the result.
  1250.  
  1251.             try
  1252.             {
  1253.                 if (timeZoneOffset < localUtcOffset)
  1254.                 {
  1255.                     return DateTime.Parse(dateTime) + absoluteOffset;
  1256.                 }
  1257.                 else
  1258.                 {
  1259.                     return DateTime.Parse(dateTime) - absoluteOffset;
  1260.                 }
  1261.             }
  1262.             catch
  1263.             {
  1264.                 CoreSystemMonitor.OperationWarning("Parsing of date time [" + dateTime + "] failed.");
  1265.                 return DateTime.MinValue;
  1266.             }
  1267.         }
  1268.  
  1269.         /// <summary>
  1270.         /// Converts a time zone (e.g. "PST") to an offset string
  1271.         /// (e.g. "-0700").
  1272.         /// </summary>
  1273.         /// <param name="tz">The time zone to convert.</param>
  1274.         /// <returns>The offset value (e.g. -0700).</returns>
  1275.         private static string GetTimeZoneOffset(string tz)
  1276.         {
  1277.             tz = tz.ToUpper();
  1278.             // If the time zone is already in number format,
  1279.             // just return it.
  1280.             if (Regex.IsMatch(tz, @"^[+-]?\d{4}$"))
  1281.             {
  1282.                 return tz;
  1283.             }
  1284.  
  1285.             string result = string.Empty;
  1286.             foreach (string[] sa in TimeZones)
  1287.             {
  1288.                 if (sa[0].ToUpper() == tz)
  1289.                 {
  1290.                     result = sa[1];
  1291.                     break;
  1292.                 }
  1293.             }
  1294.  
  1295.             return result;
  1296.         }
  1297.  
  1298.         /// <summary>
  1299.         /// Used to execute the thread pool items.
  1300.         /// </summary>
  1301.         /// <param name="state"></param>
  1302.         protected static void WaitCallbackMethod(object state)
  1303.         {
  1304.             object[] stateObjects = state as object[];
  1305.             Delegate del = ((Delegate)stateObjects[0]);
  1306.             del.Method.Invoke(del.Target, (object[])(stateObjects[1]));
  1307.         }
  1308.  
  1309.         /// <summary>
  1310.         /// Override of the full version of FireAndForgetDotNetPool().
  1311.         /// </summary>
  1312.         public static void FireAndForgetDotNetPool(CommonHelper.DefaultDelegate d)
  1313.         {
  1314.             FireAndForgetDotNetPool(d, null);
  1315.         }
  1316.  
  1317.         /// <summary>
  1318.         /// Helps to execute the delegate in a new thread, using the .NET Thread Pool.
  1319.         /// </summary>
  1320.         /// <param name="d"></param>
  1321.         /// <param name="args"></param>
  1322.         public static void FireAndForgetDotNetPool(Delegate d, params object[] args)
  1323.         {
  1324.             ThreadPool.QueueUserWorkItem(WaitCallbackMethod, new object[] { d, args });
  1325.         }
  1326.  
  1327.         /// <summary>
  1328.         /// Copy folder helper.
  1329.         /// </summary>
  1330.         static public void CopyFolder(string sourceFolder, string destFolder)
  1331.         {
  1332.             throw new Exception("Not tested.");
  1333.  
  1334.             //if (!Directory.Exists(destFolder))
  1335.             //{
  1336.             //    Directory.CreateDirectory(destFolder);
  1337.             //}
  1338.  
  1339.             //string[] files = Directory.GetFiles( sourceFolder );
  1340.             //foreach (string file in files)
  1341.             //{
  1342.             //    string name = Path.GetFileName( file );
  1343.             //    string dest = Path.Combine( destFolder, name );
  1344.             //    File.Copy( file, dest );
  1345.             //}
  1346.  
  1347.             //string[] folders = Directory.GetDirectories( sourceFolder );
  1348.             //foreach (string folder in folders)
  1349.             //{
  1350.             //    string name = Path.GetFileName( folder );
  1351.             //    string dest = Path.Combine( destFolder, name );
  1352.             //    CopyFolder( folder, dest );
  1353.             //}
  1354.         }
  1355.     }
  1356. }
Advertisement
Add Comment
Please, Sign In to add comment