123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- //-----------------------------------------------------------------------
- // <copyright company="同程网" file="GlobalFile.cs">
- // Copyright (c) V1.0
- // 作者:韦震
- // 功能:全局文件
- // 历史版本:2013-11-25 新增获取虚拟路径名
- // </copyright>
- //-----------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Text.RegularExpressions;
- namespace Ant.Common
- {
- /// <summary>
- /// 全局文件
- /// </summary>
- public class GlobalFileHelper
- {
- private static string applicationPath = string.Empty;
- /// <summary>
- /// 虚拟路径名
- /// </summary>
- public static string ApplicationPath
- {
- get
- {
- if (string.IsNullOrEmpty(applicationPath))
- {
- return applicationPath = GetPageApplicationPath();
- }
- else
- {
- return applicationPath;
- }
- }
- }
- /// <summary>
- /// 获得网站虚拟路径名称 格式是:/虚拟目录名称 .
- /// </summary>
- /// <returns></returns>
- private static string GetPageApplicationPath()
- {
- return HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath + "";
- }
- private static string isrunatweb = string.Empty;
- ///// <summary>
- ///// 检查是否在网上运行(不是本地测试)
- ///// </summary>
- //public static bool IsRunAtWeb
- //{
- // get
- // {
- // if (string.IsNullOrEmpty(isrunatweb))
- // {
- // isrunatweb = SceneryConfigureHelper.GetConfigureValue(SceneryOrderEnum.Configure.IsWebRun.ToString());
- // if (isrunatweb == null || isrunatweb == SystemEnum.TrueorFalse.isfalse.ToString("d"))
- // return false;
- // }
- // return isrunatweb == SystemEnum.TrueorFalse.istrue.ToString("d");
- // }
- //}
- #region 判断是否是本地
- /// <summary>
- /// 判断是否是本地
- /// </summary>
- /// <returns></returns>
- public static bool IsInner()
- {
- string str = HttpContext.Current.Request.Url.Host.ToLower();
- if (str.StartsWith("localhost"))
- {
- return true;
- }
- else
- {
- return IsInnerIP(str);
- }
- }
- /// <summary>
- /// 判断是否是私有IP
- /// </summary>
- /// <param name="ipAdd"></param>
- /// <returns></returns>
- public static bool IsInnerIP(string ipAdd)
- {
- bool isInnerIP = false;
- if (!string.IsNullOrEmpty(ipAdd) && IsIPAddress(ipAdd))
- {
- long ipNum = GetIpNum(ipAdd);
- long aBegin = GetIpNum("10.0.0.0");
- long aEnd = GetIpNum("10.255.255.255");
- long bBegin = GetIpNum("172.16.0.0");
- long bEnd = GetIpNum("172.31.255.255");
- long cBegin = GetIpNum("192.168.0.0");
- long cEnd = GetIpNum("192.168.255.255");
- isInnerIP = IsInIP(ipNum, cBegin, cEnd) || IsInIP(ipNum, bBegin, bEnd) || IsInIP(ipNum, aBegin, aEnd) || ipAdd == "127.0.0.1";
- }
- return isInnerIP;
- }
- /// <summary>
- /// ip转换成long
- /// </summary>
- /// <param name="ipAdd"></param>
- /// <returns></returns>
- private static long GetIpNum(string ipAdd)
- {
- string[] ipArr = ipAdd.Split('.');
- if (ipArr.Length == 4)
- {
- long a = int.Parse(ipArr[0]);
- long b = int.Parse(ipArr[1]);
- long c = int.Parse(ipArr[2]);
- long d = int.Parse(ipArr[3]);
- return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
- }
- else
- {
- return -1;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="ipAdd"></param>
- /// <param name="begin"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- private static bool IsInIP(long ipAdd, long begin, long end)
- {
- return ipAdd >= begin && ipAdd <= end;
- }
- /// <summary>
- /// 判断是否是IP地址格式
- /// </summary>
- /// <param name="str1">待判断的IP地址</param>
- /// <returns>true or false</returns>
- public static bool IsIPAddress(string str1)
- {
- if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;
- string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
- Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
- return regex.IsMatch(str1);
- }
- #endregion
- }
- }
|