GlobalFileHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //-----------------------------------------------------------------------
  2. // <copyright company="同程网" file="GlobalFile.cs">
  3. // Copyright (c) V1.0
  4. // 作者:韦震
  5. // 功能:全局文件
  6. // 历史版本:2013-11-25 新增获取虚拟路径名
  7. // </copyright>
  8. //-----------------------------------------------------------------------
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Web;
  14. using System.Text.RegularExpressions;
  15. namespace Ant.Common
  16. {
  17. /// <summary>
  18. /// 全局文件
  19. /// </summary>
  20. public class GlobalFileHelper
  21. {
  22. private static string applicationPath = string.Empty;
  23. /// <summary>
  24. /// 虚拟路径名
  25. /// </summary>
  26. public static string ApplicationPath
  27. {
  28. get
  29. {
  30. if (string.IsNullOrEmpty(applicationPath))
  31. {
  32. return applicationPath = GetPageApplicationPath();
  33. }
  34. else
  35. {
  36. return applicationPath;
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// 获得网站虚拟路径名称 格式是:/虚拟目录名称 .
  42. /// </summary>
  43. /// <returns></returns>
  44. private static string GetPageApplicationPath()
  45. {
  46. return HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath + "";
  47. }
  48. private static string isrunatweb = string.Empty;
  49. ///// <summary>
  50. ///// 检查是否在网上运行(不是本地测试)
  51. ///// </summary>
  52. //public static bool IsRunAtWeb
  53. //{
  54. // get
  55. // {
  56. // if (string.IsNullOrEmpty(isrunatweb))
  57. // {
  58. // isrunatweb = SceneryConfigureHelper.GetConfigureValue(SceneryOrderEnum.Configure.IsWebRun.ToString());
  59. // if (isrunatweb == null || isrunatweb == SystemEnum.TrueorFalse.isfalse.ToString("d"))
  60. // return false;
  61. // }
  62. // return isrunatweb == SystemEnum.TrueorFalse.istrue.ToString("d");
  63. // }
  64. //}
  65. #region 判断是否是本地
  66. /// <summary>
  67. /// 判断是否是本地
  68. /// </summary>
  69. /// <returns></returns>
  70. public static bool IsInner()
  71. {
  72. string str = HttpContext.Current.Request.Url.Host.ToLower();
  73. if (str.StartsWith("localhost"))
  74. {
  75. return true;
  76. }
  77. else
  78. {
  79. return IsInnerIP(str);
  80. }
  81. }
  82. /// <summary>
  83. /// 判断是否是私有IP
  84. /// </summary>
  85. /// <param name="ipAdd"></param>
  86. /// <returns></returns>
  87. public static bool IsInnerIP(string ipAdd)
  88. {
  89. bool isInnerIP = false;
  90. if (!string.IsNullOrEmpty(ipAdd) && IsIPAddress(ipAdd))
  91. {
  92. long ipNum = GetIpNum(ipAdd);
  93. long aBegin = GetIpNum("10.0.0.0");
  94. long aEnd = GetIpNum("10.255.255.255");
  95. long bBegin = GetIpNum("172.16.0.0");
  96. long bEnd = GetIpNum("172.31.255.255");
  97. long cBegin = GetIpNum("192.168.0.0");
  98. long cEnd = GetIpNum("192.168.255.255");
  99. isInnerIP = IsInIP(ipNum, cBegin, cEnd) || IsInIP(ipNum, bBegin, bEnd) || IsInIP(ipNum, aBegin, aEnd) || ipAdd == "127.0.0.1";
  100. }
  101. return isInnerIP;
  102. }
  103. /// <summary>
  104. /// ip转换成long
  105. /// </summary>
  106. /// <param name="ipAdd"></param>
  107. /// <returns></returns>
  108. private static long GetIpNum(string ipAdd)
  109. {
  110. string[] ipArr = ipAdd.Split('.');
  111. if (ipArr.Length == 4)
  112. {
  113. long a = int.Parse(ipArr[0]);
  114. long b = int.Parse(ipArr[1]);
  115. long c = int.Parse(ipArr[2]);
  116. long d = int.Parse(ipArr[3]);
  117. return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  118. }
  119. else
  120. {
  121. return -1;
  122. }
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <param name="ipAdd"></param>
  128. /// <param name="begin"></param>
  129. /// <param name="end"></param>
  130. /// <returns></returns>
  131. private static bool IsInIP(long ipAdd, long begin, long end)
  132. {
  133. return ipAdd >= begin && ipAdd <= end;
  134. }
  135. /// <summary>
  136. /// 判断是否是IP地址格式
  137. /// </summary>
  138. /// <param name="str1">待判断的IP地址</param>
  139. /// <returns>true or false</returns>
  140. public static bool IsIPAddress(string str1)
  141. {
  142. if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;
  143. string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
  144. Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
  145. return regex.IsMatch(str1);
  146. }
  147. #endregion
  148. }
  149. }