PublicFunction.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Text.RegularExpressions;
  6. using MySystem.Models;
  7. using Library;
  8. using Aop.Api.Util;
  9. namespace MySystem
  10. {
  11. public class PublicFunction
  12. {
  13. public static decimal NumberFormat(decimal number, int floatCount = 2)
  14. {
  15. string str = number.ToString();
  16. if (str.Contains("."))
  17. {
  18. string[] list = str.Split('.');
  19. if (list[1].Length > floatCount)
  20. {
  21. str = list[0] + "." + list[1].Substring(0, floatCount);
  22. }
  23. }
  24. else
  25. {
  26. str += ".00";
  27. }
  28. return decimal.Parse(str);
  29. }
  30. public static string GetPublicParam(WebCMSEntities db, string Key)
  31. {
  32. CustomTagSet set = db.CustomTagSet.FirstOrDefault(m => m.Tags == Key);
  33. if(set != null)
  34. {
  35. return set.Contents;
  36. }
  37. return "";
  38. }
  39. #region 获取网络文件内容
  40. public static string GetNetFileContent(string url)
  41. {
  42. string textContent = "";
  43. using (var client = new System.Net.WebClient())
  44. {
  45. try
  46. {
  47. textContent = client.DownloadString(url); // 通过 DownloadString 方法获取网页内容
  48. }
  49. catch (Exception ex)
  50. {
  51. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件内容异常");
  52. }
  53. }
  54. return textContent;
  55. }
  56. public static byte[] GetNetFileData(string url)
  57. {
  58. byte[] textContent = new byte[] { };
  59. using (var client = new System.Net.WebClient())
  60. {
  61. try
  62. {
  63. textContent = client.DownloadData(url); // 通过 DownloadString 方法获取网页内容
  64. }
  65. catch (Exception ex)
  66. {
  67. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件流异常");
  68. }
  69. }
  70. return textContent;
  71. }
  72. public static FileItem GetNetFileItem(string url)
  73. {
  74. string fileName = url;
  75. if(fileName.Contains("/"))
  76. {
  77. fileName = fileName.Substring(fileName.LastIndexOf("/") + 1);
  78. }
  79. FileItem item = new FileItem(fileName, GetNetFileData(url));
  80. return item;
  81. }
  82. #endregion
  83. }
  84. }