//-----------------------------------------------------------------------
//
// Copyright (c) V1.0
// 作者:韦震
// 功能:全局文件
// 历史版本:2013-11-25 新增获取虚拟路径名
//
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace Ant.Common
{
///
/// 全局文件
///
public class GlobalFileHelper
{
private static string applicationPath = string.Empty;
///
/// 虚拟路径名
///
public static string ApplicationPath
{
get
{
if (string.IsNullOrEmpty(applicationPath))
{
return applicationPath = GetPageApplicationPath();
}
else
{
return applicationPath;
}
}
}
///
/// 获得网站虚拟路径名称 格式是:/虚拟目录名称 .
///
///
private static string GetPageApplicationPath()
{
return HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath + "";
}
private static string isrunatweb = string.Empty;
/////
///// 检查是否在网上运行(不是本地测试)
/////
//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 判断是否是本地
///
/// 判断是否是本地
///
///
public static bool IsInner()
{
string str = HttpContext.Current.Request.Url.Host.ToLower();
if (str.StartsWith("localhost"))
{
return true;
}
else
{
return IsInnerIP(str);
}
}
///
/// 判断是否是私有IP
///
///
///
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;
}
///
/// ip转换成long
///
///
///
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;
}
}
///
///
///
///
///
///
///
private static bool IsInIP(long ipAdd, long begin, long end)
{
return ipAdd >= begin && ipAdd <= end;
}
///
/// 判断是否是IP地址格式
///
/// 待判断的IP地址
/// true or false
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
}
}