using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ManagementSystem.Utility { class Utility { #region 其它共用程式 public static bool IsNumber(string strSource) { float output; return float.TryParse(strSource, out output); } //確認是否為數字 public static bool IsDate(string strSource) { try { DateTime dtOutput; if (DateTime.TryParse(strSource, out dtOutput)) { return true; } else { return false; } } catch (Exception ex) { return false; } } //確是否為日期 public static string MarkNumber(double dbNumber, int intPoint) //將資料加上三位一撇 { return string.Format("{0:N" + intPoint + "}", dbNumber); } public static string MarkNumber(int intNumber, int intPoint) //將資料加上三位一撇 { return string.Format("{0:N" + intPoint + "}", intNumber); } public static string MarkNumber(string strNumber, int intPoint) //將資料加上三位一撇 { if (strNumber.IndexOf("%") != -1) { return strNumber; } if (!string.IsNullOrEmpty(strNumber)) { if (IsNumber(strNumber)) { return MarkNumber(Convert.ToDouble(strNumber), intPoint); } } return ""; } public static string MarkNumber(double dbNumber) //將資料加上三位一撇 { return string.Format("{0:N0}", dbNumber); } public static string MarkNumber(int intNumber) //將資料加上三位一撇 { return string.Format("{0:N0}", intNumber); } public static string MarkNumber(string strNumber) //將資料加上三位一撇 { if (!string.IsNullOrEmpty(strNumber)) { if (strNumber.IndexOf("%") != -1) { return strNumber; } if (IsNumber(strNumber)) { return MarkNumber(Math.Round(Convert.ToDouble(strNumber))); } } return ""; } public static void CreateDir(string strDirPath) //建立資料夾 { string[] strDir = strDirPath.Split('\\'); string strCreatePath = ""; foreach (string strPath in strDir) { if (strPath != "") { strCreatePath += strPath + "\\"; if (!Directory.Exists(strCreatePath)) { Directory.CreateDirectory(strCreatePath); } } } } public static void WriteFile(string strFilePath, string strContent) //撰寫檔案文字內容 { using (StreamWriter swFile = new StreamWriter(strFilePath, true)) { swFile.WriteLine(strContent); } } public static void WriteFile(string strFilePath, string[] strContent) //撰寫檔案文字內容 { //判斷檔案是否存在 string[] strPathArray = strFilePath.Split('\\'); string strCHKPath = ""; for (int intPath = 0; intPath < strPathArray.Length - 1; intPath++) { strCHKPath += strPathArray[intPath] + "\\"; } //檢查資料夾是否存在 if (!File.Exists(strCHKPath)) { CreateDir(strCHKPath); } using (StreamWriter swFile = new StreamWriter(strFilePath, true)) { foreach (string strContentText in strContent) { swFile.WriteLine(strContentText); } } } #endregion } }