You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.2 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace ManagementSystem.Utility
  8. {
  9. class Utility
  10. {
  11. #region 其它共用程式
  12. public static bool IsNumber(string strSource)
  13. {
  14. float output;
  15. return float.TryParse(strSource, out output);
  16. } //確認是否為數字
  17. public static bool IsDate(string strSource)
  18. {
  19. try
  20. {
  21. DateTime dtOutput;
  22. if (DateTime.TryParse(strSource, out dtOutput))
  23. {
  24. return true;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. return false;
  34. }
  35. } //確是否為日期
  36. public static string MarkNumber(double dbNumber, int intPoint) //將資料加上三位一撇
  37. {
  38. return string.Format("{0:N" + intPoint + "}", dbNumber);
  39. }
  40. public static string MarkNumber(int intNumber, int intPoint) //將資料加上三位一撇
  41. {
  42. return string.Format("{0:N" + intPoint + "}", intNumber);
  43. }
  44. public static string MarkNumber(string strNumber, int intPoint) //將資料加上三位一撇
  45. {
  46. if (strNumber.IndexOf("%") != -1)
  47. {
  48. return strNumber;
  49. }
  50. if (!string.IsNullOrEmpty(strNumber))
  51. {
  52. if (IsNumber(strNumber))
  53. {
  54. return MarkNumber(Convert.ToDouble(strNumber), intPoint);
  55. }
  56. }
  57. return "";
  58. }
  59. public static string MarkNumber(double dbNumber) //將資料加上三位一撇
  60. {
  61. return string.Format("{0:N0}", dbNumber);
  62. }
  63. public static string MarkNumber(int intNumber) //將資料加上三位一撇
  64. {
  65. return string.Format("{0:N0}", intNumber);
  66. }
  67. public static string MarkNumber(string strNumber) //將資料加上三位一撇
  68. {
  69. if (!string.IsNullOrEmpty(strNumber))
  70. {
  71. if (strNumber.IndexOf("%") != -1)
  72. {
  73. return strNumber;
  74. }
  75. if (IsNumber(strNumber))
  76. {
  77. return MarkNumber(Math.Round(Convert.ToDouble(strNumber)));
  78. }
  79. }
  80. return "";
  81. }
  82. public static void CreateDir(string strDirPath) //建立資料夾
  83. {
  84. string[] strDir = strDirPath.Split('\\');
  85. string strCreatePath = "";
  86. foreach (string strPath in strDir)
  87. {
  88. if (strPath != "")
  89. {
  90. strCreatePath += strPath + "\\";
  91. if (!Directory.Exists(strCreatePath))
  92. {
  93. Directory.CreateDirectory(strCreatePath);
  94. }
  95. }
  96. }
  97. }
  98. public static void WriteFile(string strFilePath, string strContent) //撰寫檔案文字內容
  99. {
  100. using (StreamWriter swFile = new StreamWriter(strFilePath, true))
  101. {
  102. swFile.WriteLine(strContent);
  103. }
  104. }
  105. public static void WriteFile(string strFilePath, string[] strContent) //撰寫檔案文字內容
  106. {
  107. //判斷檔案是否存在
  108. string[] strPathArray = strFilePath.Split('\\');
  109. string strCHKPath = "";
  110. for (int intPath = 0; intPath < strPathArray.Length - 1; intPath++)
  111. {
  112. strCHKPath += strPathArray[intPath] + "\\";
  113. }
  114. //檢查資料夾是否存在
  115. if (!File.Exists(strCHKPath))
  116. {
  117. CreateDir(strCHKPath);
  118. }
  119. using (StreamWriter swFile = new StreamWriter(strFilePath, true))
  120. {
  121. foreach (string strContentText in strContent)
  122. {
  123. swFile.WriteLine(strContentText);
  124. }
  125. }
  126. }
  127. #endregion
  128. }
  129. }