using System; using System.Data; using System.Configuration; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; namespace Utils; { public sealed class Functions { private Functions() { // // TODO: Add constructor logic here // } public static string FileContents(string FilePath) { try { FileStream file = new FileStream(FilePath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); string info = sr.ReadToEnd(); sr.Close(); file.Close(); return info; } catch (Exception ex) { string error = ex.Message; return ""; } } public static string ProperCase(string stringInput) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); bool fEmptyBefore = true; foreach (char ch in stringInput) { char chThis = ch; if (Char.IsWhiteSpace(chThis)) fEmptyBefore = true; else { if (Char.IsLetter(chThis) && fEmptyBefore) chThis = Char.ToUpper(chThis); else chThis = Char.ToLower(chThis); fEmptyBefore = false; } sb.Append(chThis); } return sb.ToString(); } public static string MoneyFormat(string sValue) { string value = sValue; string decimalValue = string.Empty; try { double newValue = Convert.ToDouble(value); if (newValue == 0) { return "$0.00"; } } catch { return "$0.00"; } // Index is zero-based string SearchString = "."; int position = value.IndexOf(SearchString); if (position < 0) { // No decimal found, return ".00" // Ex: 199 becomes 199.00 value = value + ".00"; } if (position == 0) { // Ex: .99 becomes 0.99 value = "0" + value; } if (position > 1) { string sDecimalValues = Mid(value, position + 1); // decimal found, display only last 2 decimal places if (sDecimalValues.Length == 1) { sDecimalValues = sDecimalValues + "0"; } if (sDecimalValues.Length >= 2) { sDecimalValues = Mid(sDecimalValues, 0, 2); value = Left(value, position + 1) + sDecimalValues; } } // Add comma's \\ string sCommaValue = string.Empty; int iCommaLocation = 0; if (position > 3) { if (position == 4) { // $1,000.00 value = Mid(value, 0, 1) + "," + Mid(value, 1); } if (position == 5) { // 10,000.00 value = Mid(value, 0, 2) + "," + Mid(value, 2); } if (position == 6) { // 100,000.00 value = Mid(value, 0, 3) + "," + Mid(value, 3); } if (position == 7) { // 1,000,000.00 value = Mid(value, 0, 1) + "," + Mid(value, 1, 3) + "," + Mid(value, 4); } if (position == 8) { // 10,000,000.00 value = Mid(value, 0, 2) + "," + Mid(value, 2, 3) + "," + Mid(value, 5); } if (position == 9) { // 100,000,000.00 value = Mid(value, 0, 3) + "," + Mid(value, 3, 3) + "," + Mid(value, 6); } if (position == 10) { // 1,000,000,000.00 value = Mid(value, 0, 1) + "," + Mid(value, 1, 3) + "," + Mid(value, 4, 3) + "," + Mid(value, 7); } } value = "$" + value; return value; } public static int ConvertStringToInt(string sValue) { int newValue = 0; string value = sValue; try { value = value.Trim(); newValue = Convert.ToInt16(value); return newValue; } catch { return 0; } } public static double ConvertStringToDouble(string sValue) { double newValue = 0; string value = sValue; try { value = value.Trim(); newValue = Convert.ToDouble(value); return newValue; } catch { return 0; } } public static long ConvertStringToLong(string sValue) { long newValue = 0; string value = sValue; try { value = value.Trim(); newValue = Convert.ToInt32(value); return newValue; } catch { return 0; } } public static float ConvertStringToFloat(string sValue) { float newValue = 0; string value = sValue; try { value = value.Trim(); newValue = float.Parse(value); return newValue; } catch { return 0; } } public static decimal ConvertStringToDecimal(string sValue) { decimal newValue = 0; string value = sValue; try { value = value.Trim(); newValue = decimal.Parse(value); return newValue; } catch { return 0; } } public static string Left(string param, int length) { try { string result = param.Substring(0, length); return result; } catch { return param; } } public static string Right(string param, int length) { try { string result = param.Substring(param.Length - length, length); return result; } catch { return param; } } public static string Mid(string param, int startIndex, int length) { try { // start at the specified index in the string and return N number of characters // depending on the "length" passed. string result = param.Substring(startIndex, length); return result; } catch { return param; } } public static string Mid(string param, int startIndex) { try { // start at the specified index in the string and return ALL characters. string result = param.Substring(startIndex); return result; } catch { return param; } } public static string CreateShortDescription(string myStr, int length) { string sValue = string.Empty; string sResult = myStr + ""; sResult = sResult.Trim(); try { sResult = Left(sResult, length); // check for a SPACE int iSpaceLocation = sResult.IndexOf(" "); if (iSpaceLocation > 1) { for (int i = 0; i < sResult.Length; i++) { sValue = Right(sResult, 1); if (sValue == " ") { sResult = sResult + "..."; break; } else { sResult = Left(sResult, length - (i + 1)); } } } return sResult; } catch { return myStr; } } public static bool isNumber(string value) { double newVal = 0; value = value + ""; value = value.Trim(); if (value == "") { return false; } try { newVal = Convert.ToDouble(value); return true; } catch { return false; } } public static bool isDate(string value) { DateTime dDate; value = value + ""; value = value.Trim(); if (value == "") { return false; } try { dDate = Convert.ToDateTime(value); return true; } catch { return false; } } public static bool FileLocationExists(string pRelativePath, string pFileName) { //string sCurrentDirectory = System.Environment.CurrentDirectory.ToString(); //string sPath = sCurrentDirectory + "\\" + pRelativePath; //string sTemp1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString(); string sPath = AppDomain.CurrentDomain.BaseDirectory + pRelativePath + "\\" + pFileName; bool bExists = false; try { bExists = File.Exists(sPath); } catch { bExists = false; } return bExists; } public static bool DirectoryExists(string pFullDirectoryPath) { bool exists = false; if (Directory.Exists(pFullDirectoryPath)) { exists = true; } return exists; } public static string CreateDirectory(string pDirectory) { bool success = false; try { DirectoryInfo di = Directory.CreateDirectory(pDirectory); if (Functions.DirectoryExists(pDirectory)) { return "success"; } else { return "fail"; } } catch (Exception ex) { return ex.Message.ToString(); } } public static string RemoveNonAlphaCharacters(string pValue) { string invalidCharacterList = @"\`~!@#$%^&*()-_=+[{]}|;:',<.>/?"; int invalidCharCount = invalidCharacterList.Length; string originalValue = pValue; string newValue = originalValue; if (pValue == null) { return null; } if (pValue == "") { return pValue; } if (pValue.Length < 1) { return pValue; } newValue = System.Text.RegularExpressions.Regex.Replace(newValue, @"""|\\", ""); for (int i = 0; i < invalidCharCount; i++) { string invalidChar = @Mid(invalidCharacterList, i, 1); newValue = newValue.Replace(invalidChar, ""); } newValue = System.Text.RegularExpressions.Regex.Replace(newValue, @"""|\\", ""); newValue = newValue.Trim(); return newValue; } public static string RemoveNonAlphaCharacters(string pValue, string pExcludedCharacter) { string invalidCharacterList = @"\`~!@#$%^&*()-_=+[{]}|;:',<.>/?"; int invalidCharCount = invalidCharacterList.Length; string originalValue = pValue; string newValue = originalValue; if (pValue == null) { return null; } if (pValue == "") { return pValue; } if (pValue.Length < 1) { return pValue; } newValue = System.Text.RegularExpressions.Regex.Replace(newValue, @"""|\\", ""); for (int i = 0; i < invalidCharCount; i++) { string invalidChar = @Mid(invalidCharacterList, i, 1); if (invalidChar != pExcludedCharacter) { newValue = newValue.Replace(invalidChar, ""); } } newValue = System.Text.RegularExpressions.Regex.Replace(newValue, @"""|\\", ""); newValue = newValue.Trim(); return newValue; } public static bool IsAlphabetLetter(string pValue) { string letter = pValue.ToUpper(); bool isAlphabet = false; string englishAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int location = englishAlphabet.IndexOf(pValue, 0); if (location >= 0) { isAlphabet = true; } return isAlphabet; } } }